My favorites | Sign in
Project Home Issues Source
Project Information
Members

A fluent builder on top of Apache's HttpClient to simplify its usage for common cases. This implementation is and will remain compatible with the Android API.

Usage examples

Simple GET-request

final String site = Http.get("http://somesite.com").use(client).asString();

Extended GET-request

The following example is using a RequestCustomizer (let's you modify the request just before it is being executed) and a custom (weird) ResponseHandler:

final String ok = 
    Http.get("http://somesite.com")
        .use(client)
        .header("User-Agent", "HttpClient Wrapper")
        .charset("UTF-8")
        .followRedirects(true)
        .customize(new RequestCustomizer() {
            @Override
            public void customize(final HttpUriRequest request) {
                HttpProtocolParams.useExpectContinue(request.getParams());
            }
        })
        .as(new ResponseHandler<String>() {
            @Override
            public String handleResponse(final HttpResponse response) throws IOException {
                final int statusCode = response.getStatusLine().getStatusCode();
                return statusCode == HttpStatus.SC_OK ? "YES" : "NO";
            }
        });

Simple POST-request

...sending some data to the site:

final HttpResponse response = 
    Http.post("http://somesite.com")
        .use(client)
        .data("search_name", "Mike")
        .data("search_gender", "m")
        .asResponse();

Execution methods

There are several methods to execute the request created with this builder. Here a brief overview about them:

  • .asResponse() - executes and returns the HttpResponse object
  • .asString() - executes and returns the content body as a String
  • .asFile(File) - executes and saves the stream to the given file
  • .as(ResponseHandler) - executes and returns the object created by the given ResponseHandler
  • .consumeResponse() - executes and consumes any available content on the stream
  • .throwAwayResponse() - executes and aborts immediately

See more in the code :-)

Powered by Google Project Hosting