在导入Android的一个较新的Apache HttpClient的罐子 [英] Importing a newer Apache HttpClient jar in Android

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

问题描述

我试图发送从我的Andr​​oid客户端的HTTP / HTTPS POST请求。

问题

为什么我的code失败?

至今

我创建了一个apache类/ HttpClient的电话。一切都工作得很好:

 的HttpClient HttpClient的=新DefaultHttpClient();

我读过,这种方法已经去precated,所以我切换到新的推荐方法:

 的HttpClient HttpClient的= HttpClientBuilder.create()建立()。

Eclipse中并没有这个类,所以我不得不下载Apache的HttpClient 4.3.3。我把它导入到我的项目通过复制到了文件夹并将其添加到我的构建路径(进口HttpClient的,HttpClient的缓存,的HttpCore,httpmime,流畅-HC,公地-logging,commons- codeC)。

错误信息

  2月6日至5日:15:26.946:W / dalvikvm(29587):类链接Lorg /阿帕奇/ HTTP / IMPL /康恩/ PoolingHttpClientConnectionManager;'失败
2月6日至5日:15:26.946:E / dalvikvm(29587):找不到类的org.apache.http.impl.conn.PoolingHttpClientConnectionManager,从法引用org.apache.http.impl.client.HttpClientBuilder.build

最近code

 静态私人字符串insertJson(JSON字符串,字符串URL){
    HttpClient的HttpClient的= HttpClientBuilder.create()建立()。    串responseString =;
    尝试{
        HttpPost要求=新HttpPost(URL);
        StringEntity PARAMS =新StringEntity(JSON,UTF-8);
        request.addHeader(内容类型,应用/ JSON);
        request.setEntity(PARAMS);
        HTT presponse响应= httpClient.execute(请求);
        HttpEntity实体= response.getEntity();
        responseString = EntityUtils.toString(实体,UTF-8);    }赶上(例外前){
        ex.printStackTrace();
        //这里处理异常
    } {最后
        。httpClient.getConnectionManager()关机();
    }
    返回responseString;
}


解决方案

的问题是,Android的已经包括了旧版本(<一个href=\"http://stackoverflow.com/questions/2618573/what-version-of-apache-http-client-is-bundled-in-android-1-6/4818714#4818714\">it's目前还不清楚究竟哪些中的Apache的HttpClient的,而是围绕4.0beta2)。

当您添加的新版本为您的应用程序库的罐子,在加载时APK重复类会被忽略。由于一些的新的的在HttpClient的类取决于的修改的做这些其他类,Dalvik的做什么它可以(如去除引用,和C),但除非他们被有条件地使用,这很可能造成死机。

例如,你可以看到像这样在logcat的消息:

  06-05 00:46:39.083:I / dalvikvm(6286):找不到方法org.apache.http.client.protocol.RequestDefaultHeaders&LT;初始化&gt;中被引用。从方法org.apache.http.impl.client.HttpClientBuilder.build
06-05 00:46:39.083:W / dalvikvm(6286):VFY:无法直接解决方法22794:Lorg /阿帕奇/ HTTP /客户/协议/ RequestDefaultHeaders;&LT;&初始化GT; (Ljava / UTIL /集;)V
06-05 00:46:40.434:D / dalvikvm(6286):DexOpt:找不到静态字段Lorg /阿帕奇/ HTTP / IMPL /客户/ DefaultHtt prequestRetryHandler; .INSTANCE
06-05 00:46:40.434:W / dalvikvm(6286):VFY:无法在Lorg解决静态字段8420(实例)/阿帕奇/ HTTP / IMPL /客户/ DefaultHtt prequestRetryHandler;

这特定的消息是因为 DefaultHtt prequestRetryHandler has在4.3 一个新的实例静态字段,其中it没有在Android中。还有更多的。

让我们回到你原来的问题,用新的HttpClient会因此这些名称冲突不会发生重命名的所有的类的唯一途径。这是 httpclientandroidlib 做什么。

变本加厉回:DefaultHttpClient <一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html\">has确实是德precated 在4.3,但它的不可以德$ P Android中pcated $(除非你考虑的trend倾向于使用HttpURLConnection类德precation的隐形形式 - 在这种情况下,一个新的HttpClient不会是preferred替代其一)。你到底为什么要/需要改变呢?

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

Question

Why does my code fail?

So far

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 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).

Error message

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

Most recent code

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;
}

解决方案

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

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.

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;

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.

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.

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的罐子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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