在我的项目中我将使用这个
public JSONObject post(String url, JSONObject json) throws ClientProtocolException, IOException, JSONException, ServerResponseException{
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-type","application/json");
StringEntity se = new StringEntity(json.toString());
se.setContentType("text/xml");
se.setContentEncoding( new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = client.execute(httppost);
HttpEntity entity = response.getEntity();
if(entity != null&&(response.getStatusLine().getStatusCode()==201||response.getStatusLine().getStatusCode()==200)){
//your function on output
}
else
throw new ServerResponseException(response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase());
}
这个methtod的输出是JSONObject,因为我的输出是json