异步HTTP客户端的Java [英] Asynchronous HTTP Client for Java

查看:112
本文介绍了异步HTTP客户端的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于在Java世界相对的新手,我发现很多事情令人沮丧钝来实现这一目标,在许多其他框架比较琐碎。一个主要的例子是异步HTTP请求一个简单的解决方案。眼见为一个似乎不存在,什么是最好的方法呢?创建使用阻塞型lib中像我自己的线程的HttpClient或内置Java HTTP的东西,或者我应该使用较新的非阻塞IO java的东西 - 它似乎特别复杂的东西应该是简单的

我在找的东西很容易从一个开发者角度使用 - 类似的东西的URLLoader在AS3 - 在这里您只需创建一个URLRequest - 附加一堆的事件处理程序来处理完成,错误,进度等,并调用一个方法来启动它了。

如果您不熟悉的URLLoader在AS3中,它使超级简单的,看起来是这样的:

 私人无效的getURL(字符串URL)
{
的URLLoader装载机=新的URLLoader();
loader.addEventListener(引发Event.COMPLETE,在completeHandler);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,的httpStatusHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR,事件。ioErrorHandler);

的URLRequest请求=新的URLRequest(URL);

    //火它关闭 - 这是异步的,所以我们处理
    //与事件处理完毕
loader.load(要求);
}

私人无效在completeHandler(事件事件)
{
的URLLoader装载机=(的URLLoader)将event.target;
对象的结果= loader.data;

//处理结果
}

私人无效的httpStatusHandler(事件事件)
{
//检查状态code
}

私人无效事件。ioErrorHandler(事件事件)
{
//处理错误
}
 

解决方案

使用的异步HTTP客户端以前被称为宁HTTP客户端库。 看到 的http://$c$c.ning.com/2010/03/introducing-nings-asynchronous-http-client-library/

现在可在GitHub上 https://github.com/ning/async-http-client

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

这篇关于异步HTTP客户端的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆