HttpClient 执行不断给出 ConnectTimeoutException
我的应用程序中有一个非常大的错误,我似乎无法解决。每当我通过以下代码进行休息调用时:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题出在
DefaultHttpClient
上。你是异步使用它吗?由于DefaultHttpClient
不是线程安全的,因此在异步环境中使用它可能会导致问题。当我的活动同时启动多个 Http 连接时,我之前遇到过这个问题,最后我将其更改为使用 HttpURLConnection 。您可以参考这个网站:http://www.vogella.de/articles/AndroidNetworking/article.htmlThe problem is the
DefaultHttpClient
. Are you using it asynchronously? Since theDefaultHttpClient
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 useHttpURLConnection
. You can refer to this site: http://www.vogella.de/articles/AndroidNetworking/article.html我有同样的问题。
尝试使用直接 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.
检查您是否能够使用开发计算机进行连接,并检查加载时间。您的超时很有可能太小。
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.
有类似的问题,只有在尝试同时使用多个设备连接时才能重现,解决方案是:
(c)我的服务器/实例管理
此外,还有人遇到类似的问题,并且其他一些解决方案对他们有用,例如:
Had a similar issue, could be reproduced only when trying to connect using several devices simultaneously and the solution was:
(c) My server/instance admin
Also, there are people who experience similar issues and some other solutions worked for them, for instance: