Android Gradle Apache HttpClient不存在?

Apq*_*pqu 255 android intellij-idea gradle android-studio android-gradle-plugin

我正在尝试将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
Run Code Online (Sandbox Code Playgroud)

我的build.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')
}
Run Code Online (Sandbox Code Playgroud)

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

Jin*_*inu 516

如果你在build.gradle中使用target sdk作为23添加下面的代码

android{
    compileSdkVersion 23
    buildToolsVersion '23.0.1'
    useLibrary  'org.apache.http.legacy'
}
Run Code Online (Sandbox Code Playgroud)

并将您的buildscript更改为

classpath 'com.android.tools.build:gradle:1.3.0' 
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请点击此链接

  • 将您的buildscript更改为classpath'com.android.tools.build:gradle:1.3.0' (7认同)
  • 已经有1.3.1.仍无法解析Android Studio中的类:`错误:(14,23)错误:包org.apache.http不存在 (3认同)
  • android studio 1.4 beta 2 (2认同)
  • 如果你想使用最新版本的apache http组件怎么办? (2认同)
  • Thanx Jinu,将Android Studio从1.3升级到以上版本是必须的.搞定了 (2认同)

小智 92

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

.

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

.

我通过在第二个链接中建议的build.gradle文件中添加以下内容来解决问题.

android {
    useLibrary 'org.apache.http.legacy'
}
Run Code Online (Sandbox Code Playgroud)

但是,这仅在您使用gradle 1.3.0-beta2或更高版本时才有效,因此如果您使用的是较低版本,则必须将其添加到buildscript依赖项中:

classpath 'com.android.tools.build:gradle:1.3.0-beta2'
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 不幸的是,解决方案是将目标更改为当前最高的API,这打破了很多东西!然后我不得不添加"useLibrary","classpath"PLUS参考遗留apache lib,如本问题的其他部分所述.在它再次可编译之后,我决定完全抛弃Apache代码并用URLConnection替换它,这比我想象的要容易. (2认同)

Dar*_*iya 49

将此库添加到 build.gradle

 android {
      useLibrary 'org.apache.http.legacy'
 }
Run Code Online (Sandbox Code Playgroud)

  • @Dan是因为org.apache.http库已被版本23删除. (2认同)

Ben*_*tte 38

我建议你用新的HttpURLConnection替换已弃用的apache HttpClient.

这是一个更清洁的解决方案,它很容易迁移,通常最好坚持最新的SDK更改,而不是试图破解/修补/解决方法:你通常后悔:)

步骤1

HttpGet httpGet = new HttpGet(url);
Run Code Online (Sandbox Code Playgroud)

变为:

URL urlObj = new URL(url);
Run Code Online (Sandbox Code Playgroud)

第2步

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpGet, localContext);
InputStream is = response.getEntity().getContent();
Run Code Online (Sandbox Code Playgroud)

变为:

HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
InputStream is = urlConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)

第2步之二

int status = response.getStatusLine().getStatusCode();
Run Code Online (Sandbox Code Playgroud)

变为:

int status = urlConnection.getResponseCode();
Run Code Online (Sandbox Code Playgroud)


do0*_*o01 37

副本org.apache.http.legacy.jar是在Android/Sdk/platforms/android-23/optional文件夹中,以app/libs

并将此行添加到app.gradle文件中

compile files('libs/org.apache.http.legacy.jar')
Run Code Online (Sandbox Code Playgroud)

但是如果你使用更多jar库,你可以使用这种方式

compile fileTree(dir: 'libs', include: ['*.jar'])
Run Code Online (Sandbox Code Playgroud)

  • 在我的另一个项目中,这被证明是唯一有效的答案+1并且谢谢. (2认同)

小智 13

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

useLibrary  'org.apache.http.legacy'
Run Code Online (Sandbox Code Playgroud)

到你的build.gradle文件.


Dan*_*ent 9

我遇到了类似的问题,你可能能够使用类似的方法让它工作.

首先,使用当前配置尝试此操作,httpclienthttpmime以下位置排除:

dependencies {
    compile 'com.google.android.gms:play-services:+'
    compile ('org.apache.httpcomponents:httpmime:4.2.6'){
            exclude module: 'httpclient'
        }
    compile 'org.apache.httpcomponents:httpclient:4.2.6'
} 
Run Code Online (Sandbox Code Playgroud)

就我而言,我使用以下罐子修复它:

然后,在的build.gradle,排除httpclient来自httpmime:

dependencies {
    compile 'com.google.android.gms:play-services:+'
    compile('org.apache.httpcomponents:httpmime:4.3.5') {
        exclude module: 'httpclient'
    }
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*fer 9

我遇到了同样的问题.Daniel Nugent的回答有点帮助(在听完他的建议之后HttpResponse- 但HttpClient仍然缺失了).

所以这就是为我解决的问题:

  1. (如果尚未完成,请推荐以前的进口声明)
  2. 访问http://hc.apache.org/downloads.cgi
  3. 获得4.5.1.zip从二进制部分
  4. 将它解压缩并粘贴httpcore-4.4.3httpclient-4.5.1.jarproject/libs文件夹中
  5. 右键单击jar并选择Add as library.

希望能帮助到你.


Sop*_*hia 7

Jinu和Daniel的完美答案

除此之外,如果您的compileSdkVersion为19(在我的情况下),我通过使用此方法解决了问题

compile ('org.apache.httpcomponents:httpmime:4.3'){
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile ('org.apache.httpcomponents:httpcore:4.4.1'){
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile 'commons-io:commons-io:1.3.2'
Run Code Online (Sandbox Code Playgroud)

否则,如果您的compileSdkVersion为23则使用

android {
useLibrary 'org.apache.http.legacy'
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    }
}
Run Code Online (Sandbox Code Playgroud)


NOT*_*MER 5

这就是我所做的,它对我有用.

第1步:在build.grade中添加它(模块:app)

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
Run Code Online (Sandbox Code Playgroud)

第2步:同步项目并完成.