问题描述
我正在尝试将 IntelliJ 项目转换为 Android Studio 的 Gradle 系统,但我在使用 Apache HttpClient 时遇到错误?我是否遗漏了什么,我得到的错误如下:
I am trying to convert an IntelliJ project to the Gradle system of Android Studio but I am running into errors with Apache HttpClient? Am I missing something, the errors I am getting are as follows:
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
我的 build.gradle 文件具有以下依赖项:
My build.gradle file has the following dependencies:
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 或 Google 都没有解决方案,所以我希望这个问题能帮助未来的搜索者.
It seems a lot of people are getting a similar problem but neither SO or Google have a solution so I am hoping this question will help future searchers.
推荐答案
我建议您用新的 HttpURLConnection 替换已弃用的 apache HttpClient.
I suggest you replace the deprecated apache HttpClient with the new HttpURLConnection.
这是一个更简洁的解决方案,它很容易迁移,而且通常坚持最新的 SDK 更改比尝试破解/修补/解决方法更好:您通常会在以后后悔:)
That's a cleaner solution, it's quite easy to migrate, and generally it's better to stick to the latest SDK changes than trying to hack/patch/workaround: you usually regret it later :)
第一步
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();
第 2 步之二
int status = response.getStatusLine().getStatusCode();
变成:
int status = urlConnection.getResponseCode();
这篇关于Android Gradle Apache HttpClient 不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!