码迷,mamicode.com
首页 > Web开发 > 详细

httpclient

时间:2018-07-21 14:44:49      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:miss   图片   ring   exception   iss   star   stack   使用   9.png   

技术分享图片

技术分享图片

    /*
     * 使用httpClient提交get请求
     */
    public void testClientGet(View v) {
        //1. 显示ProgressDialog
        final ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中...");
        //2. 启动分线程
        new Thread(){
            //3. 在分线程, 发送请求, 得到响应数据
            public void run() {
                try {
                    //1). 得到path, 并带上参数name=Tom1&age=11
                    String path = et_network_url.getText().toString()+"?name=Tom3&age=13";
                    
                    //2). 创建HttpClient对象
                    HttpClient httpClient = new DefaultHttpClient();
                    //3). 设置超时
                    HttpParams params = httpClient.getParams();
                    HttpConnectionParams.setConnectionTimeout(params, 5000);
                    HttpConnectionParams.setSoTimeout(params, 5000);
                    //4). 创建请求对象
                    HttpGet request = new HttpGet(path);
                    //5). 执行请求对象, 得到响应对象
                    HttpResponse response = httpClient.execute(request);
                    
                    int statusCode = response.getStatusLine().getStatusCode();
                    if(statusCode==200) {
                        //6). 得到响应体文本
                        HttpEntity entity = response.getEntity();
                        final String result = EntityUtils.toString(entity);
                        //4. 要主线程, 显示数据, 移除dialog
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                et_network_result.setText(result);
                                dialog.dismiss();
                            }
                        });
                    }
                    //7). 断开连接
                    httpClient.getConnectionManager().shutdown();
                } catch (Exception e) {
                    e.printStackTrace();
                    //如果出了异常要移除dialog
                    dialog.dismiss();
                }
            }
        }.start();
    }

    /*
     * 使用httpClient提交post请求
     */
    public void testClientPost(View v) {
        //1. 显示ProgressDialog
        final ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中...");
        //2. 启动分线程
        new Thread(){
            //3. 在分线程, 发送请求, 得到响应数据
            public void run() {
                try {
                    //1). 得到path
                    String path = et_network_url.getText().toString();
                    
                    //2). 创建HttpClient对象
                    HttpClient httpClient = new DefaultHttpClient();
                    //3). 设置超时
                    HttpParams params = httpClient.getParams();
                    HttpConnectionParams.setConnectionTimeout(params, 5000);
                    HttpConnectionParams.setSoTimeout(params, 5000);
                    //4). 创建请求对象
                    HttpPost request = new HttpPost(path);
                    //设置请求体
                    List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
                    parameters.add(new BasicNameValuePair("name", "Tom4"));
                    parameters.add(new BasicNameValuePair("age", "14"));
                    HttpEntity entity = new UrlEncodedFormEntity(parameters);
                    request.setEntity(entity);
                    
                    //5). 执行请求对象, 得到响应对象
                    HttpResponse response = httpClient.execute(request);
                    
                    int statusCode = response.getStatusLine().getStatusCode();
                    if(statusCode==200) {
                        //6). 得到响应体文本
                        entity = response.getEntity();
                        final String result = EntityUtils.toString(entity);
                        //4. 要主线程, 显示数据, 移除dialog
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                et_network_result.setText(result);
                                dialog.dismiss();
                            }
                        });
                    }
                    //7). 断开连接
                    httpClient.getConnectionManager().shutdown();
                } catch (Exception e) {
                    e.printStackTrace();
                    //如果出了异常要移除dialog
                    dialog.dismiss();
                }
            }
        }.start();
    }

 

httpclient

标签:miss   图片   ring   exception   iss   star   stack   使用   9.png   

原文地址:https://www.cnblogs.com/znsongshu/p/9345868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!