80,349
社区成员
发帖
与我相关
我的任务
分享
private String postJsonData(String url,Object obj) {
HttpClient client =null;
try {
BasicHttpParams httpParams = new BasicHttpParams();
ConnManagerParams.setTimeout(httpParams,10000);
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 20000);
//DefaultHttpClient client = new DefaultHttpClient(httpParams);
client = new DefaultHttpClient(httpParams);
//HttpClient client = new DefaultHttpClient();
//HttpParams httpParams = client.getParams();
//HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
//HttpConnectionParams.setSoTimeout(httpParams, 10000);
HttpResponse response;
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Accept", "application/json;charset=utf-8");
httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
Gson gson = new Gson();
httpPost.setEntity(new StringEntity(gson.toJson(obj), "utf-8"));//
response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity);
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
// TODO Auto-generated catch block
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
if (client != null && client.getConnectionManager() != null) {
client.getConnectionManager().shutdown();
}
}
return null;
}
String sb = postJsonData(url, ContactUsReq);
ContactUsReq contactUsReq = new ContactUsReq();
contactUsReq.setUsername(usrInfo[0]);
contactUsReq.setRandomstring(randomstring);
contactUsReq.setVerifystring(verifystring);
contactUsReq.setContent(contact);