HttpClient 执行不断给出 ConnectTimeoutException

发布于 2024-11-24 05:41:09 字数 719 浏览 0 评论 0原文

我的应用程序中有一个非常大的错误,我似乎无法解决。每当我通过以下代码进行休息调用时:

  HttpGet request = new HttpGet(url + getParams());

  HttpParams httpParameters = new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
  HttpConnectionParams.setSoTimeout(httpParameters, 10000);

  DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

  httpClient.execute(request);

我在 DDMS 中收到错误:

07-15 11:22:47.448: WARN/System.err(973): org.apache.http.conn.ConnectTimeoutException: Connect to (some ip-address) timed out

但有时代码工作完美,并且我收到了应有的数据。我还通过计算机上的普通网络浏览器测试了其余服务器调用,它总是在 100 毫秒内返回我的数据。那么我做错了什么?我还在另一台设备上测试了它,但这给了我同样的问题。如果有人能解决我的问题,我会很高兴:)

I have this very big bug in my application that I really can't seem to solve. Whenever I make a rest call via the following code:

  HttpGet request = new HttpGet(url + getParams());

  HttpParams httpParameters = new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
  HttpConnectionParams.setSoTimeout(httpParameters, 10000);

  DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

  httpClient.execute(request);

I get the error in DDMS:

07-15 11:22:47.448: WARN/System.err(973): org.apache.http.conn.ConnectTimeoutException: Connect to (some ip-address) timed out

But sometimes the code works perfect and I receive my data as it should. I also tested the rest server call via a normal webbrowser on my computer and that always gives back my data within 100ms. So what am I doing wrong? I also tested it on another device, but that gives me the same problem. I would be SO glad if somebody could solve my problem :)

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

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

发布评论

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

评论(4

病女 2024-12-01 05:41:09

问题出在 DefaultHttpClient 上。你是异步使用它吗?由于 DefaultHttpClient 不是线程安全的,因此在异步环境中使用它可能会导致问题。当我的活动同时启动多个 Http 连接时,我之前遇到过这个问题,最后我将其更改为使用 HttpURLConnection 。您可以参考这个网站:http://www.vogella.de/articles/AndroidNetworking/article.html

The problem is the DefaultHttpClient. Are you using it asynchronously? Since the DefaultHttpClient is not thread-safe, using it in an asynchronous environment might cause a problem. I've had this problem before when my activity started multiple Http connection at the same time and i ended up changing it to use HttpURLConnection. You can refer to this site: http://www.vogella.de/articles/AndroidNetworking/article.html

铃予 2024-12-01 05:41:09

我有同样的问题。

尝试使用直接 IP 来满足您的请求。我注意到 Android DNS 查找有时表现得很奇怪。我搜索并发现了这个: http://mailinglists.945824.n3.nabble.com/Android-and-reverse-DNS-lookup-issues-td3011461.html

不知道是否相关,我仍在尝试寻找解决方法。也许这里有人可以看一下并弄清楚。

I having the same issue.

Try using direct IP for your requests. I noticed that Android DNS lookups behave quite weird sometimes. I searched and found this : http://mailinglists.945824.n3.nabble.com/Android-and-reverse-DNS-lookup-issues-td3011461.html .

Dunno if it's relevant or not, i'm still trying to find a workaround. Maybe someone here can take a look and figure it out.

我还不会笑 2024-12-01 05:41:09

检查您是否能够使用开发计算机进行连接,并检查加载时间。您的超时很有可能太小。

Check if you are able to connect using your development machine, Also check the load times. There is a very good chance your timeouts are way too small.

时光无声 2024-12-01 05:41:09

有类似的问题,只有在尝试同时使用多个设备连接时才能重现,解决方案是:

实例重新启动解决了连接问题。
(或者只需要重新启动网络服务)

(c)我的服务器/实例管理

此外,还有人遇到类似的问题,并且其他一些解决方案对他们有用,例如:

  1. 禁用 tcp_timestamp
  2. 禁用 tcp_tw_reuse & 禁用 tcp_timestamp 禁用 tcp_tw_reuse tcp_tw_recycle
  3. 在 Android 清单中启用 vm 保存模式: android:vmSafeMode="true"

Had a similar issue, could be reproduced only when trying to connect using several devices simultaneously and the solution was:

Instance reboot solved problem with connections.
(or just need to restart networking service)

(c) My server/instance admin

Also, there are people who experience similar issues and some other solutions worked for them, for instance:

  1. Disable tcp_timestamp
  2. Disable tcp_tw_reuse & tcp_tw_recycle
  3. Enabled vm save mode in Android manifest: android:vmSafeMode="true"

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