Import new Apache HttpClient banner into Android

I am trying to send an HTTP / HTTPS send request from my Android client.

Question

Why is my code not working?

Until

I made a call to the apache / HttpClient class. Everything worked perfectly:

HttpClient httpClient = new DefaultHttpClient(); 

I read that this method is deprecated, so I switched to the new recommended method:

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

Eclipse did not have this class, so I had to download Apache HttpClient 4.3.3. I imported it into my project by copying it to the libs folder and adding 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 

Latest 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; } 
+6
source share
2 answers

The problem is that Android already includes an older version ( not clear which one , but around 4.0beta2) of Apache HttpClient.

When you add new version banks as libraries of your application, duplicate classes are ignored when loading the APK. Since some of the new classes in HttpClient depend on modifications made to these other classes, dalvik does what it can (for example, removing links, and c), but if they are not used conditionally, it can cause crashes.

For example, you can see such messages 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 is a specific post because DefaultHttpRequestRetryHandler has a new static INSTANCE field in 4.3 , which it does not have on Android . There are many more.

Returning to the original question, the only way to use the new httpclient is to rename all classes so that these name conflicts do not occur. This is what httpclientandroidlib is .

Even further back: DefaultHttpClient is really deprecated in version 4.3, but not deprecated on Android (unless you are considering a trend

+5
source

The answer is one year, the correct answer is to use the official android port for Apache libraries, which uses a different namespace and therefore avoids conflicts: https://hc.apache.org/httpcomponents-client-4.5.x/android-port. html

0
source

Source: https://habr.com/ru/post/970347/


All Articles