首页 > 代码库 > DefaultHttpClient execute() 方法报错
DefaultHttpClient execute() 方法报错
今天写代码,这里报错
public static String doGet(String url, boolean useCache, String encoding)
throws Exception {
if (TextUtils.isEmpty(url)) {
throw new NullPointerException("网络请求地址不能为空");
}
String data = http://www.mamicode.com/null;
if (useCache) {
data = http://www.mamicode.com/doGetCache(url);
if (!LFormat.isEmpty(data)) {
return data;
}
}
DefaultHttpClient httpClient = new DefaultHttpClient(getClientParams());
HttpGet httpGet = new HttpGet(url);
if (!LFormat.isEmpty(LApplication.getInstance().getSessionValue())) {
httpGet.setHeader(LApplication.getInstance().getSessionKey(),
LApplication.getInstance().getSessionValue());
}
HttpResponse httpResponse;
httpResponse = httpClient.execute(httpGet); //这里报错
HttpEntity httpEntity = httpResponse.getEntity();
if (httpResponse.getStatusLine().getStatusCode() == 200) {
InputStream inputStream = httpEntity.getContent();
data = http://www.mamicode.com/convertStreamToString(inputStream, encoding);
if (LFormat.isEmpty(data))
throw new ConnectException(RUNTIME_EXCEPTION);
data = http://www.mamicode.com/LFormat.JSONTokener(data);
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
for (int i = 0; i < cookies.size(); i++) {
if (LApplication.getInstance().getSessionKey()
.equals(cookies.get(i).getName())) {
LApplication.getInstance().setSessionValue(
cookies.get(i).getValue());
}
}
} else {
throw new ConnectException(RUNTIME_EXCEPTION);
}
if (useCache && !LFormat.isEqual(SEND_ERROR, data)) {
doSetCache(url, data);
}
return data;
}
.解决方法很简单
Manifest清单文件中加上网络协议
<uses-permission android:name="android.permission.INTERNET" />
DefaultHttpClient execute() 方法报错