您的位置:首页 > 移动开发 > Android开发

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

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