Android GET方式和POST方式提交给WEB服务器

一、GET方式:
public void testRequestByGET(){
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("http://192.168.241.1:8080/test/servlet/ManageServlet?title='");
			sb.append(URLEncoder.encode("中国", "UTF-8"));
			sb.append("'&time=10");
			URL url = new URL(sb.toString());
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(5000);
			conn.setRequestMethod("GET");
			if(conn.getResponseCode() == 200){
				Log.i(TAG, "success");
			} else {
				Log.i(TAG, "fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

二、POST方式:

public void save(){
		try {
			String path = new String("http://172.30.1.114:8080/test/servlet/ManageServlet");
			Map<String, String> params = new HashMap<String, String>();
			params.put("title", "少年时代");
			params.put("time", String.valueOf(10));
			// sendPOSTRequest(path, params, "UTF-8");
<span style="white-space:pre">			</span>sendHttpClientPOSTRequest(path, params, "UTF-8");
<span style="white-space:pre">		</span>} catch (Exception e) {e.printStackTrace();}}

</pre><pre code_snippet_id="538522" snippet_file_name="blog_20141201_2_5669117" name="code" class="java">
	private boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
		StringBuilder sb = new StringBuilder();
		if(params != null && !params.isEmpty()){
			for(Map.Entry<String, String> entry : params.entrySet()){
				sb.append(entry.getKey()).append("=");
				sb.append(URLEncoder.encode(entry.getValue(), encoding));	// 使用URLENcoder.encode进行编码
				sb.append("&");
			}
			sb.deleteCharAt(sb.length()-1);	// 	去掉最后一个“&”
			byte[] entry = sb.toString().getBytes();	// 得到实体数据
			HttpURLConnection conn = (HttpURLConnection)(new URL(path)).openConnection();
			conn.setConnectTimeout(5000);
			conn.setRequestMethod("POST");
			conn.setDoOutput(true);		// 允许往外输出数据流
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			conn.setRequestProperty("Content-Length", String.valueOf(entry.length));
			OutputStream outputStream = conn.getOutputStream();
			outputStream.write(entry);
			if(conn.getResponseCode() == 200){
				Log.i(TAG, "success");
				return true;
			}
		}
		Log.i(TAG, "fail");
		return false;
	}

/**
	 * 第二种方法:使用HttpClient发送POST请求
	 * @return
	 * @throws Exception 
	 */
	public boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();	// 存放请求参数
		StringBuilder sb = new StringBuilder();
		if(params != null && !params.isEmpty()){
			for(Map.Entry<String, String> entry : params.entrySet()){
				pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
		}
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encoding);
		HttpPost httpPost = new HttpPost(path);
		httpPost.setEntity(entity);
		DefaultHttpClient httpClient = new DefaultHttpClient();	// DefaultHttpClient:看做浏览器
		HttpResponse response = httpClient.execute(httpPost);
		if(response.getStatusLine().getStatusCode() == 200){
			return true;
		}
		return false;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值