Android studio中“import org.apache.http.xxx; 不可用

来源:互联网 发布:php 访问 编辑:程序博客网 时间:2024/04/28 07:22

前言

今天在使用旧项目已经封装好的java文件到AS的时候,文件里面的import org.apache.http.xxx几乎都是不可以通过的,百度了一下,原来是Android M启动了反荡妇机制,移除了Apache HTTP导致的,
这里写图片描述

解决办法

Android Studio2.0以上只需要进行3、4步即可
1、在gradle-wrapper.properties中配置使用较新版本的gradle(建议不要在已有的代码上修改,建议注释原有的代码,再添加下面一行代码)

distributionUrl=https\://services.gradle.org/distributions/gradle-2.6-all.zip

2、在build.gradle中使用较新版本的gradle buildtools

buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:1.3.0'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}

3、添加以下依赖,重新使用已经deprecated 的apache http 包:

android {    useLibrary 'org.apache.http.legacy'//这行代码最重要了    ....}

4、添加apache http component 的依赖,补全缺失的类,比如Header:

dependencies {    compile 'org.apache.httpcomponents:httpcore:4.4.2'}

最后结果:

这里写图片描述


引用

[1] Android studio中“import org.apache.http.Header; 不可用 - jiangtianhao13269230的专栏 - 博客频道 - CSDN.NET
[2] Android M 起默认移除了Apache HTTP

0 0