The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required .class files

在 Android 6.0(API 23)中,Google 已经移除了移除了Apache HttpClient 相关的类、HttpResponse 类。缺失jar包使用HttpResponse等会报错:
The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required

转载请注明出处:http://shenshanlaoyuan.com/2016/06/13/Android/The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required .class files/

访问原文「The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required .class files」获取最佳阅读体验并参与讨论

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//获取网络数据
HttpUtils httpUtils = new HttpUtils();
//发送URL请求
httpUtils.send(HttpMethod.GET, MyConstants.NEWSCENTERURL, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
//访问数据成功
String jsonData = responseInfo.result;
System.out.println(jsonData);
}
@Override
public void onFailure(HttpException error, String msg) {
//访问数据失败
System.out.println("网络请求数据失败:" + error);
}
});

解决方法

推荐使用HttpUrlConnection,如果要继续使用需要Apache HttpClient,需要在eclipse下libs里添加org.apache.http.legacy.jar。添加方法如下:

  • Eclipse中 在错误原因上点击ctrl+1,选择Configure build path,或者Project->Properties->Java Build Path->Libraries->Add Ecternal JARS->你的SDK目录的
  • android studio里在相应的module下的build.gradle中加入:
1
2
3
android {
useLibrary 'org.apache.http.legacy'
}
深山老猿 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
如对你有帮助,请我喝杯咖啡吧!
显示 Gitment 评论