Android四种联网方式

第一HttpURLConnection 的GET请求:

public void testConnectionGet(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=Tom1&age=11";
                //2). 创建URL对象
                URL url = new URL(path);
                //3). 打开连接, 得到HttpURLConnection对象
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                //4). 设置请求方式,连接超时, 读取数据超时
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(6000);
                //5). 连接服务器
                connection.connect();
                //6). 发请求, 得到响应数据
                    //得到响应码, 必须是200才读取
                int responseCode = connection.getResponseCode();
                if(responseCode==200) {
                    //得到InputStream, 并读取成String
                    InputStream is = connection.getInputStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    while((len=is.read(buffer))!=-1) {
                        baos.write(buffer, 0, len);
                    }
                    final String result = baos.toString();

                    baos.close();
                    is.close();

                    //4. 在主线程, 显示得到的结果, 移除dialog
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            et_network_result.setText(result);
                            dialog.dismiss();
                        }
                    });
                }
                //7). 断开连接
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
                //如果出了异常要移除dialog
                dialog.dismiss();
            }
        }
    }.start();
}
第二种 HttpURLConnection 的POST请求

public void testConnectionPost(View v) {
    //1. 显示ProgressDialog
    final ProgressDialog dialog = ProgressDialog.show(this, null, "正在加载中...");
    //2. 启动分线程
    new Thread(new Runnable() {
        //3. 在分线程, 发送请求, 得到响应数据
        @Override
        public void run() {
            try {
                //1). 得到path
                String path = et_network_url.getText().toString();
                //2). 创建URL对象
                URL url = new URL(path);
                //3). 打开连接, 得到HttpURLConnection对象
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                //4). 设置请求方式,连接超时, 读取数据超时
                connection.setRequestMethod("POST");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                //5). 连接服务器
                connection.connect();
                //6). 发请求, 得到响应数据
                    //得到输出流, 写请求体:name=Tom1&age=11
                OutputStream os = connection.getOutputStream();
                String data = "name=Tom2&age=12";
                os.write(data.getBytes("utf-8"));
                    //得到响应码, 必须是200才读取
                int responseCode = connection.getResponseCode();
                if(responseCode==200) {
                    //得到InputStream, 并读取成String
                    InputStream is = connection.getInputStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    while((len=is.read(buffer))!=-1) {
                        baos.write(buffer, 0, len);
                    }
                    final String result = baos.toString();

                    baos.close();
                    is.close();

                    //4. 在主线程, 显示得到的结果, 移除dialog
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            et_network_result.setText(result);
                            dialog.dismiss();
                        }
                    });
                }
                os.close();
                //7). 断开连接
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
                dialog.dismiss();
            }
        }
    }).start();
}

第三种 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();
}




  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值