Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacement for deprecated DefaultHttpClient

I am using DefaultHttpClient in my current app.

I read this article which states that the DefaultHttpClient is deprecated: http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html

It points to this website: http://android-developers.blogspot.com/2011/09/androids-http-clients.html?m=1 which is too old and written in 2011.

I am going to drop using DefaultHttpClient and follow this article which uses Apache’s HttpClient: http://loopj.com/android-async-http/

I wonder if this is the right path to take when programming in 2015 targeting android API 19 and above.

like image 462
Ehsan Avatar asked Aug 04 '15 06:08

Ehsan


People also ask

What is org Apache HttpComponents?

The Apache HttpComponents project is responsible for creating and maintaining a toolset of low level Java components focused on HTTP and associated protocols. This project functions under the Apache Software Foundation (http://www.apache.org), and is part of a larger community of developers and users.

What is HttpClientBuilder in Java?

java.lang.Object org.apache.http.impl.client.HttpClientBuilder public class HttpClientBuilder extends Object. Builder for CloseableHttpClient instances. When a particular component is not explicitly set this class will use its default implementation.

What is closeable HttpClient?

CloseableHttpClient is the base class of the httpclient library, the one all implementations use. Other subclasses are for the most part deprecated. The HttpClient is an interface for this class and other classes. You should then use the CloseableHttpClient in your code, and create it using the HttpClientBuilder .


1 Answers

Ok I waited for a week and so and did all lots of researches. I think I have found the answer.

I strongly advise for beginners and even professional Android programmers to know the existence of very helpful library called Retrofit:

Extensive document is present at http://square.github.io/retrofit/

The Stack overflow also has samples for almost everything one need to do over a network to contact a remote REST service.

It is better to stop using HttpURLConnection and AsyncTask. Retrofit is way faster and supports all failure situations out of the box.

like image 84
Ehsan Avatar answered Nov 15 '22 22:11

Ehsan