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

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

2014-07-05 16:16 681 查看
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐