您的位置:首页 > 理论基础 > 计算机网络

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:连接处理,关闭输入流,关闭连接。

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;
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: