Android HTTPPOST方法请求数据
protected static CommResult HttpPost(Context context, String url,
HashMap<String, String> map) {
synchronized ("http post") {
CommResult result = new CommResult();
HttpClient httpClient = getNewHttpClient(context);
HttpPost httpPost = new HttpPost(url);
ArrayList<BasicNameValuePair> postDate = new ArrayList<BasicNameValuePair>();
Set<String> set = map.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
postDate.add(new BasicNameValuePair(key, map.get(key)));
}
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
postDate, HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
InputStream in = response.getEntity().getContent();
int statusCode = response.getStatusLine().getStatusCode();
String message = InputStreamToString(in);
result.setMessage(message);
result.setResponseCode(String.valueOf(statusCode));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2013-03/80178.htm
protected static CommResult HttpPost(Context context, String url,
HashMap<String, String> map) {
synchronized ("http post") {
CommResult result = new CommResult();
HttpClient httpClient = getNewHttpClient(context);
HttpPost httpPost = new HttpPost(url);
ArrayList<BasicNameValuePair> postDate = new ArrayList<BasicNameValuePair>();
Set<String> set = map.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
postDate.add(new BasicNameValuePair(key, map.get(key)));
}
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
postDate, HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
InputStream in = response.getEntity().getContent();
int statusCode = response.getStatusLine().getStatusCode();
String message = InputStreamToString(in);
result.setMessage(message);
result.setResponseCode(String.valueOf(statusCode));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2013-03/80178.htm