【发布时间】:2018-11-03 20:05:21
【问题描述】:
**我有这段代码,我想将 Httpclient 更改为 HttpURLConnection,我的 setEntity 方法有问题,我没有找到 HttpURLConnection 的等价物 **
public JSONObject postData(JSONObject jOb) throws Throwable {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://www.blabla");
try {
httppost.setEntity(new StringEntity(jOb.toString()));
HttpResponse response = httpclient.execute(httppost);
String responseText = EntityUtils.toString(response.getEntity());
return new JSONObject(responseText);
} catch (Throwable e) {
ControlTable.logErrors(e.toString() + "\t" + jOb.toString(), 32);
throw e;
}
}
【问题讨论】:
-
根据我的经验,Apache HTTP 客户端比 HttpUrlConnection 更好,更实用。尤其是错误处理对于 HttpURLConnection 来说真的很痛苦。
标签: android httprequest httpurlconnection