Android中的post和get请求(HttpURLConnection)方式
2014-10-13 13:36
921 查看
Post和get请求是和服务器请求的两种最常用方式,(在J***AEE中,servlet中包含这两个响应处理)。
使用get方式:
get是请求内容跟在网址后的,不放在请求实体当中(post则相反)。
操作get请求,一般有如下几个步骤:
step1:声明URL;
step2:声明HttpURLConnection;
step3:设置HttpURLConnection属性;
step4:获取输入流InputStreamReader;
step5:数据采集处理,使用BufferedReader的readLine()比较普遍。
step6:连接处理,关闭输入流,关闭连接。
使用Post方式完成请求:
操作Post请求,一般包含以下步骤:
step1:声明URL;
step2:声明HttpURLConnection;
step3:设置HttpURLConnection属性;
step4:提交输入流,写入请求实体参数(比get多了这个步骤);
step5:获取输入流InputStreamReader;
step7:数据采集处理,使用BufferedReader的readLine()比较普遍。
step8:连接处理,关闭输入流,关闭连接。
使用get方式:
get是请求内容跟在网址后的,不放在请求实体当中(post则相反)。
操作get请求,一般有如下几个步骤:
step1:声明URL;
step2:声明HttpURLConnection;
step3:设置HttpURLConnection属性;
step4:获取输入流InputStreamReader;
step5:数据采集处理,使用BufferedReader的readLine()比较普遍。
step6:连接处理,关闭输入流,关闭连接。
public static String httpConnectionGet(String strURL) { // 服务器端返回数据值 String resultData = ""; URL url = null; try { url = new URL(strURL); // 使用HttpURLConnection打开连接 HttpURLConnection urlConn = null; // 得到读取的内容 InputStreamReader in = null; try { urlConn = (HttpURLConnection) url.openConnection(); // 设置连接超时 urlConn.setConnectTimeout(CONN_TIMEOUT_TIME); urlConn.setReadTimeout(CONN_TIMEOUT_TIME); in = new InputStreamReader(urlConn.getInputStream()); // 为输出创建BufferedReader BufferedReader buffer = new BufferedReader(in); String inputLine = ""; while ((inputLine = buffer.readLine()) != null) { resultData += inputLine + "\n"; } } catch (IOException e) { Log.i("DDD", "IO流异常"); resultData = ResponseConstants.NET_ERROR; e.printStackTrace(); } finally { if (in != null) try { in.close(); } catch (IOException e) { Log.i("DDD", "IO流关闭失败"); resultData = ResponseConstants.NET_ERROR; e.printStackTrace(); } if (urlConn != null) urlConn.disconnect(); } } catch (MalformedURLException e) { Log.i("DDD", "URL解析错误"); resultData = ResponseConstants.NET_ERROR; e.printStackTrace(); } return resultData; }
使用Post方式完成请求:
操作Post请求,一般包含以下步骤:
step1:声明URL;
step2:声明HttpURLConnection;
step3:设置HttpURLConnection属性;
step4:提交输入流,写入请求实体参数(比get多了这个步骤);
step5:获取输入流InputStreamReader;
step7:数据采集处理,使用BufferedReader的readLine()比较普遍。
step8:连接处理,关闭输入流,关闭连接。
public static String httpConnectionPost(String strURL, String strParam) { System.out.println("strURL:" + strURL); // 服务器端返回数据值 String resultData = ""; try { URL url = new URL(strURL); // 使用HttpURLConnection打开连接 HttpURLConnection urlConn = null; DataOutputStream out = null; BufferedReader reader = null; try { urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setDoInput(true); // 设置以POST方式 urlConn.setRequestMethod("POST"); // 设置连接超时 urlConn.setConnectTimeout(CONN_TIMEOUT_TIME); urlConn.setReadTimeout(CONN_TIMEOUT_TIME); // Post 请求不能使用缓存 urlConn.setUseCaches(false); urlConn.setInstanceFollowRedirects(true); // 设置指定的请求头字段的值 urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); urlConn.setRequestProperty("Charset", "utf-8"); urlConn.connect(); out = new DataOutputStream(urlConn.getOutputStream()); // 编码设置 // URLEncoder.encode(strParam, "UTF_8"); // 将要上传的内容写入流中 out.writeBytes(strParam); out.flush(); // 获取数据 reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); String inputLine = null; while (((inputLine = reader.readLine()) != null)) { resultData += inputLine + "\n"; } } catch (IOException e) { Log.i("DDD", "IO流异常"); resultData = ResponseConstants.NET_ERROR; e.printStackTrace(); } finally { try { if (out != null) out.close(); if (reader != null) reader.close(); } catch (IOException e) { Log.i("DDD", "IO流关闭失败"); resultData = ResponseConstants.NET_ERROR; e.printStackTrace(); } if (urlConn != null) urlConn.disconnect(); } } catch (MalformedURLException e) { Log.i("DDD", "URL解析错误"); resultData = ResponseConstants.NET_ERROR; e.printStackTrace(); } System.out.println("jsonResult:" + resultData); return resultData; }
相关文章推荐
- Android之HttpURLConnection的GET和POST请求
- HttpUrlConnection两种请求数据的方式get和post提交数据
- 使用HttpURLConnection和使用HttpClient方式请求网络采用get方式和post方式请求数据
- 使用HttpURLConnection采用get方式或post方式请求数据
- android(21)(使用HttpURLConnection,HttpClient和AsyncHttpClient分别实现post和get请求数据)
- Android-----HttpURLConnection的 get 请求和 post 请求
- 【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- Http学习之使用HttpURLConnection发送post和get请求 android
- HttpURLConnection get post 方式请求 (笔记)
- java发送post和get请求(1)---HttpURLConnection方式
- HttpUrlConnection和HttpClient两种方式获取GET和POST请求
- Android_HttpURLConnection_Get和Post请求
- Android---------HttpURLConnection的 get 请求和 post 请求(更容易理解)
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- Android_HttpURLConnection_Get和Post请求
- httpURLConnection-网络请求的两种方式-get请求和post请求
- Android例子—HttpURLConnection发送POST、GET请求代码示例
- Android HttpURLConnection获取网络下载图片POST请求方式
- (转)Android_HttpURLConnection_Get和Post请求