Copyright (C) 2012 Pixmob (http://github.com/pixmob)
Android provides two methods for making Http(s) requests: HttpURLConnection and Apache HttpClient.
The Android development team recommends the use of HttpURLConnection for new applications, as this interface is getting better over Android releases.
However, using HttpURLConnection on earlier Android devices (before ICS) is troublesome because of the underlying implementation bugs:
- connection pool poisoning
- missing SSL certificates on some Android devices
- slow POST requests
- headers in lower-case
- incomplete read
- transparent Gzip compression
The framework Pixmob HttpClient (PHC) provides a clean interface to HttpURLConnection, with workarounds for known bugs, from Android 2.1 to Android 4.0.
Using PHC will make your code easier to understand, while leveraging the Android native network layer.
Making Http requests with HCL is easy.
HttpClient hc = new HttpClient();
hc.get("http://www.google.com").execute();
Downloading a file cannot be easier:
File logoFile = new File(context.getCacheDir(), "logo.png");
hc.get("http://www.mysite.com/logo.png").toFile(logoFile).execute();
You may want to send POST requests:
// the response buffer is reusable across requests (GC friendly)
final StringBuilder buf = new StringBuilder(64);
hc.post("http://www.mysite.com/query").setParameter("q", "hello").setHandler(
new HttpResponseHandler() {
public void onResponse(HttpResponse response) throws Exception {
response.read(buf);
System.out.println(buf);
}
}
).execute();
Please read JavaDoc and source code for advanced use.
A sample application (with source code) is available in the Play Store: HCL Demo.
All of the source code in this project is licensed under the Apache 2.0 license except as noted.
This project is actually a library project.