Android使用HttpPost向服务器发送Json数据

String retSrc = "";
String path = "http://www.27442.com/login";
JSONObject jsonObject = new JSONObject();
HttpPost httpPost = new HttpPost(path);
httpPost.setHeader("Content-Type",
"application/x-www-form-urlencoded; charset=utf-8");

try {
//Post参数
jsonObject.put("userName","zhangsan");
jsonObject.put("passWord","123456");

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("param", jsonObject.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
//获取HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 获取HttpResponse实例
HttpResponse httpResp = httpClient.execute(httpPost);
retSrc = EntityUtils.toString(httpResp.getEntity());
} catch (Exception e) {
e.printStackTrace();
}
return retSrc;


发布了12 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/zyjzyj55/article/details/37054771