Skip to content

bretdabaker/httpclient

 
 

Repository files navigation

Using HttpUrlConnection with Android 2.1+ devices

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:

  1. connection pool poisoning
  2. missing SSL certificates on some Android devices
  3. slow POST requests
  4. headers in lower-case
  5. incomplete read
  6. 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.

Usage

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.

License

All of the source code in this project is licensed under the Apache 2.0 license except as noted.

How to use this library in my project?

This project is actually a library project.

About

Wrapper library around Android's HttpUrlConnection.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 98.2%
  • JavaScript 1.8%