套接字超时使得使用DefaultHTTPClient HTTPGET请求时

编程入门 行业动态 更新时间:2024-10-26 01:27:39
本文介绍了套接字超时使得使用DefaultHTTPClient HTTPGET请求时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有哪些优点和缺点背后重用 DefaultHTTPClient 从Android应用程序发送HTTP请求时到外部服务器?我试图用重复使用 DefaultHTTPClient 制作时,周期性 HTTPGET 的要求,但我得到随机插座超时(特别是使用3G的时候)

What are the pros and cons behind reusing DefaultHTTPClient when sending HTTP requests from an Android app to an external server? I tried using reusing a DefaultHTTPClient when making periodic HTTPGet requests, but I get random socket timeouts (specially when using 3G).

我的code看起来如下:

My code looks like follows:

public class MyHTTPSender { private DefaultHTTPClient mClient; public MyHTTPSender() { mClient = new DefaultHTTPClient(); } public void send(String httpAddress) { HttpGet get = new HttpGet(this.surrogateURL); HttpResponse response = null; try { response = httpClient.execute(get); // ... consume entity if OK } catch (Exception e) { } finally { if (response != null) { // do some sanity checks to ensure Entity is there! response.getEntity().consumeContent(); } } } }

我看不出什么问题,我在做什么。我有一个单独的处理程序,使HTTPPost请求,并且工作得很好(使用不同的DefaultHTTPClient对象)。

I can't see anything wrong with what I am doing. I have a separate handler that make HTTPPost requests, and that works perfectly well (uses a different DefaultHTTPClient object).

有什么建议?

推荐答案

什么API级别是你吗?

What API level are you on?

如果你是在8级以上,你可能会考虑尝试 AndroidHttpClient ,它可以指定一个较好的插座超时。

If you're on 8 or above you might consider trying AndroidHttpClient, which may have a better socket timeouts specified.

否则,你可能会检查你如何创建 DefaultHttpClient 并尝试指定更长的超时。

Otherwise, you might examine how you're creating the DefaultHttpClient and try specifying longer timeouts.

更多推荐

套接字超时使得使用DefaultHTTPClient HTTPGET请求时

本文发布于:2023-08-07 03:47:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316795.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DefaultHTTPClient   HTTPGET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!