在 Android 中导入更新的 Apache HttpClient jar [英] Importing a newer Apache HttpClient jar in Android

查看:92
本文介绍了在 Android 中导入更新的 Apache HttpClient jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 Android 客户端发送 HTTP/HTTPS 发布请求.

I'm trying to send an HTTP/HTTPS post request from my Android client.

为什么我的代码会失败?

Why does my code fail?

我创建了一个 apache 类/HttpClient 调用.一切正常:

I created an apache class/HttpClient call. Everything worked fine:

HttpClient httpClient = new DefaultHttpClient();

我了解到此方法已被弃用,因此我已切换到新的推荐方法:

I've read that this method has been deprecated, so I've switched to the new recommended method:

HttpClient httpClient = HttpClientBuilder.create().build();

Eclipse 没有这个类,所以我不得不下载 Apache HttpClient 4.3.3.我通过将它复制到 libs 文件夹并将它们添加到我的构建路径(导入的 httpclient、httpclient-cache、httpcore、httpmime、fluent-hc、commons-logging、commons-codec).

Eclipse didn't have this class, so I had to download Apache HttpClient 4.3.3. I imported it to my project by copying it into the libs folder and adding them to my build path (imported httpclient, httpclient-cache, httpcore, httpmime, fluent-hc, commons-logging, commons-codec).

06-05 02:15:26.946: W/dalvikvm(29587): Link of class 'Lorg/apache/http/impl/conn/PoolingHttpClientConnectionManager;' failed
06-05 02:15:26.946: E/dalvikvm(29587): Could not find class 'org.apache.http.impl.conn.PoolingHttpClientConnectionManager', referenced from method org.apache.http.impl.client.HttpClientBuilder.build

最近的代码

static private String insertJson(String json,String url){
    HttpClient httpClient = HttpClientBuilder.create().build();

    String responseString = "";
    try {
        HttpPost request = new HttpPost(url);
        StringEntity params =new StringEntity(json, "UTF-8");
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        responseString = EntityUtils.toString(entity, "UTF-8");

    }catch (Exception ex) {
        ex.printStackTrace();
        // handle exception here
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
    return responseString;
}

推荐答案

问题是 Android 已经包含一个旧版本 (目前还不清楚具体是哪个,但大约是 Apache HttpClient 的 4.0beta2).

The problem is that Android already includes an older version (it's unclear exactly which, but around 4.0beta2) of Apache HttpClient.

当您将新版本的 jars 添加为应用程序库时,加载 APK 时会忽略重复的类.由于 HttpClient 中的一些类依赖于对这些其他类所做的修改,dalvik 会尽其所能(例如删除引用,&c),但除非有条件地使用它们,这很可能会导致崩溃.

When you add the jars of the new version as libraries of your application, duplicate classes are ignored when the APK is loaded. Since some new classes in HttpClient depend on modifications made on these other classes, dalvik does what it can (e.g. removing references, &c) but unless they are used conditionally, this is likely to cause crashes.

例如,您可以在 logcat 中看到这样的消息:

For example, you can see messages like these in logcat:

06-05 00:46:39.083: I/dalvikvm(6286): Could not find method org.apache.http.client.protocol.RequestDefaultHeaders.<init>, referenced from method org.apache.http.impl.client.HttpClientBuilder.build
06-05 00:46:39.083: W/dalvikvm(6286): VFY: unable to resolve direct method 22794: Lorg/apache/http/client/protocol/RequestDefaultHeaders;.<init> (Ljava/util/Collection;)V
06-05 00:46:40.434: D/dalvikvm(6286): DexOpt: couldn't find static field Lorg/apache/http/impl/client/DefaultHttpRequestRetryHandler;.INSTANCE
06-05 00:46:40.434: W/dalvikvm(6286): VFY: unable to resolve static field 8420 (INSTANCE) in Lorg/apache/http/impl/client/DefaultHttpRequestRetryHandler;

这个特殊的消息是因为 DefaultHttpRequestRetryHandler 在 4.3 中有一个新的 INSTANCE 静态字段,它Android 中没有.还有很多.

This particular message is because DefaultHttpRequestRetryHandler has a new INSTANCE static field in 4.3, which it does not have in Android. There are many more.

回到您最初的问题,使用较新的 httpclient 的唯一方法是重命名 所有 类,以免发生这些名称冲突.这就是 httpclientandroidlib 所做的.

Going back to your original question, the only way to use a newer httpclient would be to rename all classes so these name conflicts don't occur. This is what httpclientandroidlib does.

更进一步: DefaultHttpClient 确实已在 4.3 中弃用,但在 Android 中弃用(除非您考虑 趋势使用 HttpUrlConnection 一种隐蔽的弃用形式——在这种情况下,较新的 HttpClient 也不是首选的替代方案).为什么你想要/需要改变它?

Going even further back: DefaultHttpClient has indeed been deprecated in 4.3, but it is not deprecated in Android (unless you consider the trend towards using HttpUrlConnection a stealth form of deprecation -- in which case a newer HttpClient wouldn't be the preferred alternative either). Why exactly would you want/need to change it?

这篇关于在 Android 中导入更新的 Apache HttpClient jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆