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
}
Use the "Async Http Client" formerly called ning http client library. See http://code.ning.com/2010/03/introducing-nings-asynchronous-http-client-library/
Now Available at GitHub https://github.com/ning/async-http-client