在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

[java]​ view plain​​​ ​​ copy​​​ ​ 


  1. //获取服务器Json数据
  2. HttpUtils httpUtils = new HttpUtils();

  3. //发送URL请求
  4. httpUtils.send(HttpMethod.GET,Myconstants.NEWCENTERURL,new RequestCallBack<String>() {

  5. @Override//访问数据成功
  6. public void onSuccess(ResponseInfo<String> responseInfo) {
  7. String jsonData =<u><span style="color:#ff0000;"> responseInfo</span></u>.result;
  8. System.out.println(jsonData);
  9. }

  10. @Override//访问数据失败
  11. public void onFailure(HttpException error, String msg) {
  12. System.out.println("网络请求数据失败"+error);
  13. }
  14. });


[java]​ view plain​​​ ​​ copy​​​  


  1. //获取服务器Json数据
  2. HttpUtils httpUtils = new HttpUtils();

  3. //发送URL请求
  4. httpUtils.send(HttpMethod.GET,Myconstants.NEWCENTERURL,new RequestCallBack<String>() {

  5. @Override//访问数据成功
  6. public void onSuccess(ResponseInfo<String> responseInfo) {
  7. String jsonData =<u><span style="color:#ff0000;"> responseInfo</span></u>.result;
  8. System.out.println(jsonData);
  9. }

  10. @Override//访问数据失败
  11. public void onFailure(HttpException error, String msg) {
  12. System.out.println("网络请求数据失败"+error);
  13. }
  14. });


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

1>、Eclipse中 在错误原因上点击ctrl+1,选择Configure build path,或者Project->Properties->Java Build Path->Libraries->Add Ecternal JARS->你的SDK目录的

\platforms\android-23\optional\org.apache.http.legacy.jar->ok

The type org.apache.http.HttpResponse cannot be resolved. It is indirectly referenced from required_apache

2>、android studio里在相应的module下的build.gradle中加入:

android {

useLibrary 'org.apache.http.legacy'

}