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.
DefaultHttpClient is at a higher level of abstraction than HttpUrlConnection. Either one should be fine based on your needs. If you dont need the control of HttpUrlConnection, stick with the DefaultHttpClient.
Related
My task is to load web site in WebView over HTTPS with unsupported by vanilla Android TLS cipher.
Right now as proof of concept I have implemented apache http client capable of doing http requests to such resources.
What is the best approach to make WebView use my custom client implementation to do all network requests?
Actually, the accepted answer is wrong. You do not get full information; what's missing are request bodies.
So you can implement GET or HEAD requests just fine, but POST requests are trickier.
I haven't seen a good solution for that yet. One I've come across uses JavaScript inserted into the page to collect the POST data, hand it to Java via a binding https://developer.android.com/guide/webapps/webview.html#BindingJavaScript and then performs the request in Java.
Unfortunately, the WebView will try to perform the same request again, so you need to add more hackery to make it work.
Since Android 5.0 (API 21+) you can use WebViewClient.shouldInterceptRequest(WebView, WebResourceRequest) to intercept web requests with full information and perform them with custom HTTP client.
Thanks #Stan for the tip.
I am writing a Java GAE app and relying on URL Fetch to send HTTP requests to other servers (see here. Up until this point, I had been using their HttpUrlConnection interface implementation in order to use the URL Fetch service. However, I am rewriting my app to use the low-level API instead of HttpUrlConnection because I want to send asynchronous HTTP requests.
My question: What is the URL Fetch low level API equivalent of HttpUrlConnection.getResponseMessage():String? Based on what I see from the low-level API javadoc for HTTPResponse, I am not sure of what method I should use. I can get the response code alright, but I'd like to read a useful message along with the code, especially for when an error occurs.
Thanks.
Is there much difference between those two and which one is preferably to use?
Based on the Android source code:
https://github.com/CyanogenMod/android_frameworks_base/blob/gingerbread/core/java/android/net/http/AndroidHttpClient.java#L106
AndroidHttpClient is set to do the following extra settings:
Turn off stale checking, since the connections can break all the time.
Set ConnectionTimeout and SoTimeout (20 or 60 seconds)
Turn off redirecting.
Use a session cache for SSL sockets.
Use gzip compressed traffic between client and server if it's possible.
Doesn't retain cookies by default.
AndroidHttpClient: Subclass of the Apache DefaultHttpClient that is configured with reasonable default settings and registered schemes for Android, and also lets the user add HttpRequestInterceptor classes.
This client processes cookies but does not retain them by default. To retain cookies, simply add a cookie store to the HttpContext
[API]
This interesting blog post from an android developer gives an overview of the different Android’s HTTP clients.
According to this post, URLConnection should be preferred over DefaultHttpClient or AndroidHttpClient on Gingerbread and above.
When I Get "https://login.facebook.com" by using Chrome, the response is 302 and redirect me to http://www.facebook.com. However, when I get "https://login.facebook.com" by using HttpClient's GetMethod, it will be blocked for a long time and finally throws a Timeout Exception.
I feel really frustrating. Is the implementation of Sun JDK's SSL which HttpClient is based on not compatible with facebook's login server?
Can any one tell me what should I do to connect https://login.facebook.com by using HttpClient.
You should use the facebook api for java for doing so. I think they block requests like yours, since they have made an api for developers to log in through.
You need to join the facebook developers group and create an application so you can get an API key. Se link below for more information.
Download it here
I need to download images from a website, and I have the login name and password, but if i just use URL to download the image, it will throw a exception: there is no value in session.
I think I need to login the website before I can programmatically download the image.
Do you have any solutions ? Thanks in advance !
In simple circumstances you can use a URLConnection with the URL and stream the contents down. More generally I'd strongly advise you use Apache HttpClient since you'll need to do authentication and possibly receive and send cookies to the server. Read the user guide regarding Authentication and Methods, particularly Get.
Use the HTTP Client libraries in order to write a spider for content access.
I would suggest to record the HTTP traffic for login and content access and then rebuild the communication using the library, if you want to stick with Java.
There are other libraries as well for other languages like Perl:LWP.
Although the java.net package provides basic functionality for accessing resources via HTTP, it doesn't provide the full flexibility or functionality needed by many applications. HttpClient seeks to fill this void by providing an efficient, up-to-date, and feature-rich package implementing the client side of the most recent HTTP standards and recommendations.
Designed for extension while providing robust support for the base HTTP protocol, HttpClient may be of interest to anyone building HTTP-aware client applications such as web browsers, web service clients, or systems that leverage or extend the HTTP protocol for distributed communication.
HTTPClient
HTTPClient Authentication
I'd like to mention HtmlUnit. It is a headless browser with Javascript for Java.