In building web service designed to to interact with mobile devices, I am not sure what the best approach is for implementing HTTP requests on Android.
I came across this post, which finishes by stating HttpURLConnection
is the preferred method for making HTTP requests, and I have had success using the HttpsURLConnection
.
When searching for answers or reading other sample code (even fairly recent posts), all seem to use DefaultHttpClient
, which seems to go against the official word from Google.
I am trying to future proof my Android application as much as possible. With that in mind, is the HttpURLConnection
the best choice?
If you are supporting 2.2 as well, Best approach may be to utilize both DefaultHttpClient or HttpURLConnection
if (Integer.parseInt(Build.VERSION.SDK) <= Build.VERSION_CODES.FROYO) {
// Use DefaultHttpClient here
}
else{
//use HttpURLConnection
}
Reason: HttpURLConnection is more stable after Froyo while DefaultHttpClient is less buggy in froyo and lesser version.
Ref : http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html Android includes two HTTP clients: HttpURLConnection and Apache HTTP Client. Both support HTTPS, streaming uploads and downloads, configurable timeouts, IPv6 and connection pooling. Apache HTTP client has fewer bugs in Android 2.2 (Froyo) and earlier releases. For Android 2.3 (Gingerbread) and later, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. See the Android Developers Blog for a comparison of the two HTTP clients.
It really matters which version of Android you are using. Take a look at http://android-developers.blogspot.com/2011/09/androids-http-clients.html for some guidance from Google.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With