我试图将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和谷歌都没有解决方案,所以我希望这个问题能帮助未来的搜索者。


当前回答

我遇到了这个问题,然后找到了这些页面: 在这里您可以看到apache库已弃用,但它没有被删除,所以它应该可以工作。它不是。

See.

在这里您可以看到如何将apache库包含到您的项目中

See.

我通过在我的构建中添加以下内容来解决这个问题。Gradle文件推荐在第二个链接。

android {
    useLibrary 'org.apache.http.legacy'
}

然而,这只在你使用gradle 1.3.0-beta2或更高版本时有效,所以如果你在一个较低的版本上,你必须将这个添加到buildscript依赖项中:

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

其他回答

我遇到了这个问题,然后找到了这些页面: 在这里您可以看到apache库已弃用,但它没有被删除,所以它应该可以工作。它不是。

See.

在这里您可以看到如何将apache库包含到您的项目中

See.

我通过在我的构建中添加以下内容来解决这个问题。Gradle文件推荐在第二个链接。

android {
    useLibrary 'org.apache.http.legacy'
}

然而,这只在你使用gradle 1.3.0-beta2或更高版本时有效,所以如果你在一个较低的版本上,你必须将这个添加到buildscript依赖项中:

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

我也遇到了同样的问题。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。

希望能有所帮助。

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

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

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

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

将这个库添加到build.gradle中

 android {
      useLibrary 'org.apache.http.legacy'
 }

基本上你所需要做的就是添加:

useLibrary  'org.apache.http.legacy'

适合你的身材。gradle文件。