使用HttpClient的POST方式传JSON格式的参数给Restful服务

阅读更多

需要支持的JAR包:httpclient-4.2.5,httpcore-4.3

1、Android/java:

 

JSONObject jsonObj = new JSONObject();

jsonObj.put("userName ", userName);

jsonObj.put("passWord", passWord);

// Create the POST object and add the parameters

HttpPost httpPost = new HttpPost(url);

StringEntity entity = new StringEntity(jsonObj.toString(), "UTF-8");

entity.setContentType("application/json");

httpPost.setEntity(entity);

HttpClient client = new DefaultHttpClient();

HttpResponse response = client.execute(httpPost);

 

2、用jQuery:

var data = {};

 

data.userName = "zhangsan";

data.passWord= "12345";

 

var myData=$.toJSON(data);

$.ajax({

  url:url,

  type:"POST",

  data:myData,

  contentType:"application/json; charset=utf-8",

  dataType:"json",

  success: function(){

    ...

  }

})

你可能感兴趣的:(使用HttpClient的POST方式传JSON格式的参数给Restful服务)