Alex Rivera | Logout

Asynchronous HTTP Client for Java

Asked 2009-06-18T18:53:43.050
20

As a relative newbie in the Java world, I am finding many things frustratingly obtuse to accomplish that are relatively trivial in many other frameworks. A primary example is a simple solution for asynchronous http requests. Seeing as one doesn't seem to already exist, what is the best approach? Creating my own threads using a blocking type lib like httpclient or the built-in java http stuff, or should I use the newer non-blocking io java stuff - it seems particularly complex for something which should be simple.

What I am looking for is something easy to use from a developer point of view - something similar to URLLoader in AS3 - where you simply create a URLRequest - attach a bunch of event handlers to handle the completion, errors, progress, etc, and call a method to fire it off.

If you are not familiar with URLLoader in AS3, its so super easy and looks something like this:

private void getURL(String url)
{
    URLLoader loader = new URLLoader();
    loader.addEventListener(Event.Complete, completeHandler);
    loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
    loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

    URLRequest request = new URLRequest(url);

    // fire it off - this is asynchronous so we handle
    // completion with event handlers
    loader.load(request);
}

private void completeHandler(Event event)
{
    URLLoader loader = (URLLoader)event.target;
    Object results = loader.data;

    // process results
}

private void httpStatusHandler(Event event)
{
    // check status code
}

private void ioErrorHandler(Event event)
{
    // handle errors
}
Edit
Report

2 Answers

5

Looks like you want (a part of) NIO -- there's a good tutorial here, the asynchronous networking part starts at p. 30 and there are many useful links at the end.

answered 2009-06-18T19:01:05.300
0

Asyncweb provides an asynchronous http client along with its http server. Available for download from the following location:

https://svn.apache.org/repos/asf/mina/asyncweb/trunk

answered 2010-07-29T12:21:49.530

Your Answer