Android Deprecated Apache Module (Httpclient, Httpresponse, etc.)

HttpClient is deprecated (android studio, Target=api 22)

Stop using it and use URLConnection instead. It's been 4 years Google recommends this.
http://android-developers.blogspot.be/2011/09/androids-http-clients.html

If you want an external library with a nicer API, you can try OkHttp: http://square.github.io/okhttp/

No more support for Apache HttpClient in marshmallow

Use HttpURLConnection instead.

Or some libraries like:

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

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

OkHttp: http://square.github.io/okhttp/

Read this article about comparision: Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley

Deprecated Java HttpClient - How hard can it be?

Relevant imports:

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;

Usage:

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

EDIT (after Jules' suggestion):

As the build() method returns a CloseableHttpClient which is-a AutoClosable, you can place the declaration in a try-with-resources statement (Java 7+):

try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

// use httpClient (no need to close it explicitly)

} catch (IOException e) {

// handle

}


Related Topics



Leave a reply



Submit