我试图将IntelliJ项目转换为Android Studio的Gradle系统,但我遇到了Apache HttpClient错误?我是否遗漏了一些东西,我得到的错误如下:

Error:(10, 30) error: package org.apache.http.client does not exist
Error:(11, 30) error: package org.apache.http.client does not exist
Error:(12, 37) error: package org.apache.http.client.entity does not exist
Error:(13, 38) error: package org.apache.http.client.methods does not exist
Error:(14, 38) error: package org.apache.http.client.methods does not exist
Error:(15, 38) error: package org.apache.http.client.methods does not exist
Error:(16, 35) error: package org.apache.http.impl.client does not exist
Error:(134, 33) error: cannot find symbol class HttpUriRequest
Error:(164, 39) error: cannot find symbol class HttpUriRequest
Error:(106, 17) error: cannot find symbol class HttpGet
Error:(106, 39) error: cannot find symbol class HttpGet
Error:(117, 17) error: cannot find symbol class HttpPost
Error:(117, 40) error: cannot find symbol class HttpPost
Error:(125, 43) error: cannot find symbol class UrlEncodedFormEntity
Error:(135, 9) error: cannot find symbol class HttpClient
Error:(135, 33) error: cannot find symbol class DefaultHttpClient
Error:(155, 18) error: cannot find symbol class ClientProtocolException
Error:(165, 9) error: cannot find symbol class HttpClient
Error:(165, 33) error: cannot find symbol class DefaultHttpClient
Error:(185, 18) error: cannot find symbol class ClientProtocolException

我的建立。Gradle文件有以下依赖项:

dependencies {
    compile 'com.google.android.gms:play-services:+'
    compile 'org.apache.httpcomponents:httpclient:4.2.6'
    compile 'org.apache.httpcomponents:httpmime:4.2.6'
    compile files('libs/core.jar')
}

似乎很多人都遇到了类似的问题,但SO和谷歌都没有解决方案,所以我希望这个问题能帮助未来的搜索者。


当前回答

我不得不发帖,因为上面的答案都不完全适合我。

我正在使用Android Studio

classpath 'com.android.tools.build:gradle:1.5.0'

compileSdkVersion 23
buildToolsVersion "23.0.3"

步骤1:下载最新的jar文件(http://www-eu.apache.org/dist//httpcomponents/httpclient/binary/httpcomponents-client-4.5.2-bin.zip)

第二步:复制粘贴.jar文件到你的模块(可以是app或library)中的libs文件夹(如果不存在就创建)

步骤3:右键单击jar并“添加为库”。它会自动将jar文件作为依赖项添加到模块的gradle文件中

第4步:现在你的问题会自动得到解决,但如果你在你的应用程序中使用proguard,它会给你关于重复类文件的警告,不会让你构建。这是一个已知的错误,您需要在您的proguard-rules中添加以下内容

-dontwarn org.apache.commons.**
-keep class org.apache.http.** { *; }
-dontwarn org.apache.http.**

好运!

其他回答

我也遇到了同样的问题。Daniel Nugent的回答帮了一点忙(在遵循他的建议之后找到了HttpResponse -但HttpClient仍然缺失)。

这就是我解决问题的方法:

(如果还没有完成,推荐之前的import-statements) 访问http://hc.apache.org/downloads.cgi 从二进制部分获取4.5.1.zip 解压并将httpcore-4.4.3 & httpclient-4.5.1.jar粘贴到project/libs文件夹中 右键单击jar并选择Add as library。

希望能有所帮助。

如果你使用目标SDK为23,在build.gradle中添加以下代码

android{
    compileSdkVersion 23
    buildToolsVersion '23.0.1'
    useLibrary  'org.apache.http.legacy'
}

并将构建脚本更改为

classpath 'com.android.tools.build:gradle:1.3.0' 

欲了解更多信息,请点击此链接

这就是我所做的,而且对我很有效。

步骤1:将此添加到构建中。年级(模块:应用)

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

步骤2:同步项目并完成。

我不得不发帖,因为上面的答案都不完全适合我。

我正在使用Android Studio

classpath 'com.android.tools.build:gradle:1.5.0'

compileSdkVersion 23
buildToolsVersion "23.0.3"

步骤1:下载最新的jar文件(http://www-eu.apache.org/dist//httpcomponents/httpclient/binary/httpcomponents-client-4.5.2-bin.zip)

第二步:复制粘贴.jar文件到你的模块(可以是app或library)中的libs文件夹(如果不存在就创建)

步骤3:右键单击jar并“添加为库”。它会自动将jar文件作为依赖项添加到模块的gradle文件中

第4步:现在你的问题会自动得到解决,但如果你在你的应用程序中使用proguard,它会给你关于重复类文件的警告,不会让你构建。这是一个已知的错误,您需要在您的proguard-rules中添加以下内容

-dontwarn org.apache.commons.**
-keep class org.apache.http.** { *; }
-dontwarn org.apache.http.**

好运!

在这个问题上花了几天时间后,我终于设法解决了它。不幸的是,我们需要它,如果我们想使用扩展文件。这里的一些人已经提供了解决方案,但对我来说,答案遗漏了一些小细节,可以为我节省很多时间。下面是事件的顺序,像我这样的新手可以节省你宝贵的时间。

首先,我从D:\Users\Imre\AppData\Local\Android\sdk1\extras\谷歌\play_licensing导入“library”文件夹,进入文件,新建,导入模块,然后导航到“library”文件夹。如果你打开SDK管理器,点击弹出屏幕底部的“启动独立SDK管理器”,你可以将指针悬停在底部的“Extras”文件夹上,黄色的小信息会告诉你在哪里可以找到你需要导入的包。

Once that is done, go down to the "library" section in the left pane with "Project" and within it the "Android" tab open. Open the java section of the library and open the APKExpansionPolicy class. If you have errors and the import org.apache.http.NameValuePair and the import org.apache.http.client.utils.URLEncodedUtils are pale grey, open the build.gradle(Project:whatever) and make sure that in "buildscript" under "dependancies" you have the "classpath 'com.android.tools.build:gradle:1.5.0" included. The 1.5.0 might be different in your case. I guess it depends on your Studio version. Mine is 1.5.1. If yours is newer, you will be reminded to update the numbers.

在此之后,转到“build.gradle(Module:library)”,并包含“useLibrary”。“Legacy”进入“android”部分。应提供立即同步(R/H上角)。这样做。

If you get further error messages (I wouldn't know it because I did it the other way round), the Apache files might be missing from your project, as apache is not supported anymore. Find them in C/D:Users/Yourname/AppData/Local/Android/Sdk/platforms/android-23/optional and copy/paste them into yourproject/app/libs folder. You should have an optional.json and an org.apache.http.legacy.jar file in there. You might be better off going to apache's website and dowloading the newest version: http://hc.apache.org/downloads.cgi Unzip it anywhere you want, open it, go to "lib", copy the httpclient-4.5.1.jar file and replace the org.apache.http.legacy.jar file with it.

同步并重新构建/清理您的项目,它应该可以运行了。

Just in case, open your terminal (L/H bottom of Android Studio) and type in "gradlew clean". It will install a few things. Once it is done, type in "gradlew assemble". It will take a few minutes to finish, but it will give you the errors if you have any of course. If you cannot type in anything in the Terminal, launch command prompt (Windows button+R), type in cmd, hit OK, right click on the title of the small black popup screen (C:\WINDOWS\system32\cmd.exe), Properties, Options, in the bottom of the window check that "Use legacy console" is thicked. Restart A.Studio.

好运!