【问题标题】:Cannot import AndroidHttpClient;无法导入 AndroidHttpClient;
【发布时间】:2016-05-29 13:24:59
【问题描述】:

我正在使用 Lynda 的 2014 年教程学习 android restful web services。代码使用这个 httpManager 类:

   import android.net.http.AndroidHttpClient;

    public class HttpManager {

        public static String getData(String uri) {
            HttpClient client = AndroidHttpClient.AndroidHttpClient.netInstance("AndroidAgaent");
            HttpGet request = New HttpGet(uri);
            HttpResponse response;

            try {
                response = client.execute(request);
                return EntiyUtils.toString(response.getEntinity());
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                client.close();
            }
        }
    }

然而,android studio 说:

无法解析符号“AndroidHttpClient”

我设置了minSdkVersion 8,但仍然无法解决问题。

我想知道如何解决这个问题,或者如果该类已被丢弃,如何将代码替换为现成支持的当前类?

【问题讨论】:

  • 感谢 CommonsWare 的提示。那么 okhttp 还是你的个人推荐吗?
  • 如果您将自己限制在“开箱即用的当前类”,您唯一的选择是HttpURLConnection。如果你愿意使用库,我推荐 Square 系列的 HTTP 库:OkHttp3(通用 HTTP)、Retrofit(Web 服务)和 Picasso(图像)。
  • square 家族与 Volly 相比如何?听起来很不错:code.tutsplus.com/tutorials/…
  • 排球没问题。它得到了谷歌的混合支持和很少的文档,这就是我不推荐它的原因。

标签: android androidhttpclient


【解决方案1】:

我认为AndroidHttpClient现在已经被弃用了,试试新的HTTP,它可用并且比以前的更强大!

  1. http://square.github.io/retrofit/

  2. https://github.com/koush/ion

  3. https://developer.android.com/training/volley/index.html

个人建议改造

【讨论】:

  • 您能否提供上述使用 Retrofit 的类的替代代码?
【解决方案2】:

如果您使用的是 Android Studio,请在您的 gradle 构建中添加这些依赖项:

compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 2011-07-05
    • 2012-06-12
    • 1970-01-01
    • 2021-11-20
    • 2016-03-27
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多