用于大响应的 http 异步响应

发布于 2024-12-26 04:19:54 字数 342 浏览 0 评论 0原文

我熟悉 android HTTPURLConnection 和 apache HTTPConnection 类及其工作方式(它们都是同步的,但我可以接受)。

我有一个很大的响应,其中包含来自服务器的许多行数据。这是一个 JSON 响应,我可以在解析所有响应之前部分显示数据。一些 json 解析器允许这样做(例如 xcers 允许 xml)。上面提到的两个类相关的回调和方法允许吗?当我在打开输入流并读取时收到 HTTPURLConnection 的响应时,当全部数据已经存在时我是否打开流?或者我可以打开并阅读它以及接下来的更多内容吗?

另外,android上有没有可以与NIO一起使用的http方法?

I'm familiar with android HTTPURLConnection and apache HTTPConnection classes and the way they work (they are all synchronous, but I can live with that).

I have a large response with many lines of data comming from the server. It's a JSON response and I can display the data partially before I parsed all the response. Some json parsers allow that (like xcers allows for xml). Do the callbacks and methods related to the two classes mentioned above allow it? When I get the response from HTTPURLConnection upon opening input stream and read, do I open the stream when ALL the data is already there? Or can I open and read it and more that should follow?

Also, is there any http method on android that works with NIO?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2025-01-02 04:19:54

使用 HttpClient,当您像这样打开响应流

HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();

并开始读取时,您实际上开始下载,并且在收到这些字节后立即获得新字节。您无需等待所有内容下载完成即可开始阅读。

据我所知,与Android捆绑的HttpClient不是基于NIO的。我不知道有什么替代方法可以做到这一点。

With HttpClient, when you open the response stream like this:

HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();

and start reading, you actually start the downloading and you get new bytes as soon as these are received. You don't wait for everything to get downloaded to start reading.

As far as I know the HttpClient that is bundled with Android is not based on NIO. I don't know of any alternative that does so.

悲凉≈ 2025-01-02 04:19:54

除了 Ladlestein 评论中的所有可能解决方案之外,还有一个简单的答案:将所有内容包装在 AsyncTask 中。这是示例项目演示如何执行HTTP请求 在 AsyncTask 中使用 HttpClient

In addition to all of the possible solutions in Ladlestein's comment, there's the simple answer of wrapping all that in an AsyncTask. Here is a sample project demonstrating doing an HTTP request using HttpClient in an AsyncTask.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文