Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

actionscript 3 - Asynchronous HTTP Client for Java

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...