Android四种联网方式
16lz
2021-01-25
第一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 parameters = new ArrayList(); 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();}
更多相关文章
- “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
- Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- Android(安卓)aidl学习笔记-客户端
- 如何把批量数据导入到android 的 sqlite 数据库
- Android常用控件之GridView使用BaseAdapter
- Android官方架构组件ViewModel+LiveData+DataBinding架构属于自
- Android通用布局对象
- Android(安卓)EventBus 架构设计