我试图将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和谷歌都没有解决方案,所以我希望这个问题能帮助未来的搜索者。
我建议你用新的HttpURLConnection替换已弃用的apache HttpClient。
这是一个更干净的解决方案,很容易迁移,通常最好坚持最新的SDK更改,而不是试图破解/修补/变通:你通常会后悔:)
步骤1
HttpGet httpGet = new HttpGet(url);
就变成:
URL urlObj = new URL(url);
步骤2
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpGet, localContext);
InputStream is = response.getEntity().getContent();
就变成:
HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
InputStream is = urlConnection.getInputStream();
一步两步
int status = response.getStatusLine().getStatusCode();
就变成:
int status = urlConnection.getResponseCode();
我建议你用新的HttpURLConnection替换已弃用的apache HttpClient。
这是一个更干净的解决方案,很容易迁移,通常最好坚持最新的SDK更改,而不是试图破解/修补/变通:你通常会后悔:)
步骤1
HttpGet httpGet = new HttpGet(url);
就变成:
URL urlObj = new URL(url);
步骤2
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpGet, localContext);
InputStream is = response.getEntity().getContent();
就变成:
HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
InputStream is = urlConnection.getInputStream();
一步两步
int status = response.getStatusLine().getStatusCode();
就变成:
int status = urlConnection.getResponseCode();
我不得不发帖,因为上面的答案都不完全适合我。
我正在使用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.**
好运!