Org.apache.http.entity.FileEntity deprecated in Android 6 (Marshmallow)

I upgrade the application to API 23 where org.apache.http deprecated.

My current (deprecated) code is as follows:

 HttpClient httpClient = new DefaultHttpClient(); File file = new File(attr.Value); String url = server_url; HttpPost request = new HttpPost(url); FileEntity fileEntity = new FileEntity(file, "image/png"); request.setEntity(fileEntity); HttpResponse response = httpClient.execute(request); String output = getContent(response.getEntity().getContent()); 

I found some suggestions on how to do this with HttpURLConnection , but they are all much more complicated than the current solution (which can no longer be used). I am talking about many lines of code to perform the same functions as above.

Examples: this page and this page

Does anyone have a good solid solution?

+6
source share
3 answers

If you change your compileSdkVersion to 21, your application will be compiled. However, there are reasons why Google is abandoning its native implementation of HttpClient, so you should probably continue to work on another library. What "some other library" might be:

In particular, OkHttp seems to have a pretty good API for posting a file and posting a multi-page form , which should be similar to what your HttpClient code does.

+8
source

The Apache HttpClient 4.3 port for Android is designed to correct the situation by providing official releases compatible with Google Android.

Given that with the Android API 23 Google fork HttpClient was the removal of this project was discontinued.

Those users who want to continue to use Apache HttpClient on Android are encouraged to consider

Apache HttpClient 4.3 port for Android when configuring Android API 22 and older

 dependencies { compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' } 

Apache HttpClient for Android packages supported by Marek Sebera when setting up Android API 23 and later

 dependencies { compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1' } 

taken from Apache Official Website : Apache HttpClient for Android

NOTE. You do not need to use the useLibrary 'org.apache.http.legacy' , which was introduced for projects that were not ported from the Android HttpClient classes. For further explanation .

+3
source

The best replacement for HTTPClient is to use Volley. It is much easier to use, processes request queues and caches requests. It is fully compatible with almost all API levels up to API 4.

See the Android documentation for how to do this.

0
source

All Articles