public static String doPostJson(String url, String param) {
String responseBody = "";
HttpPost httppost = new HttpPost(url);
StringEntity entities = new StringEntity(param, "UTF-8");
entities.setContentEncoding("UTF-8");
entities.setContentType("application/x-www-form-urlencoded");
httppost.setEntity(entities);
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
public String handleResponse(final HttpResponse response)
throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity)
: null;
} else {
throw new ClientProtocolException(
"Unexpected response status: " + status);
}
}
};
try {
responseBody = new DefaultHttpClient().execute(httppost, responseHandler);
} catch (SocketTimeoutException e) {
e.printStackTrace();
responseBody = "timeout";
return responseBody;
} catch (ClientProtocolException e) {
e.printStackTrace();
return responseBody;
} catch (IOException e) {
e.printStackTrace();
return responseBody;
}
return responseBody;
}