一、Httpclient發送json請求
public String RequestJsonPost(String url){
??? String strresponse = null;
??? try{
??????? HttpClient hc = new DefaultHttpClient();
?????? HttpPost hp = new HttpPost(url);
?????? JSONObject jsonParam = new JSONObject();
?????? jsonParam.put("user","admin");
?????? jsonParam.put("password", "123456");
?????? //設置數據為utf-8編碼
?????? StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");
?????? //設置請求編碼
?????? entity.setContentEncoding("utf-8");
?????? //設置請求類型
?????? entity.setContentType("application/json");
?????? hp.setEntity(entity);
?????? //請求并得到結果
?????? HttpResponse result = hc.execute(hp);
?????? strresponse = EntityUtils.toString(result.getEntity(),"utf-8").trim();
??? }catch(Exception e){
?????? e.printStackTrace();
}
return strresponse;
}
?