HttpClient的执行一直给ConnectTimeoutException [英] HttpClient execute keeps giving ConnectTimeoutException

查看:1599
本文介绍了HttpClient的执行一直给ConnectTimeoutException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这非常大的错误,在我的应用程序,我真的似乎无法来解决。每当我通过以下code进行REST调用:

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);

我得到DDMS错误:

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

但有时候code完美的作品,我收到我的数据,因为它应该。我也通过正常的网页浏览器进行测试,其余服务器调用我的电脑上,而且总是给回我的数据在100ms以内。所以我在做什么错了?我也测试了它在其它设备上,但是这给了我同样的问题。我会很高兴,如果有人能解决我的问题:)

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 :)

推荐答案

问题是 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

这篇关于HttpClient的执行一直给ConnectTimeoutException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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