日期:2014-05-17 浏览次数:20562 次
public static String doPost(JSONObject json,String method,String url) throws IOException, JSONException {
// String url = Constants.BASE_URL+method;
Log.d(Tag, "url is "+url);
HttpPost httpPost = new HttpPost(url);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
if (json != null) {
@SuppressWarnings("unchecked")
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
params.add(new BasicNameValuePair(key, json.getString(key)));
Log.d(Tag, "key is "+key+" value is "+json.getString(key));
}
}
HttpEntity httpEntity = new UrlEncodedFormEntity(params,
Constants.UTF8);
httpPost.setEntity(httpEntity);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpConnectionParams.setConnectionTimeout(httpPost.getParams(), 5000);
HttpConnectionParams.setSoTimeout(httpPost.getParams(), 10000);
Log.d(Tag, "状态码:“"+httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return Constants.CONNECSUC;
}else {return Constants.CONNECTFAIL;
}
}