首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接超时

连接超时
EN

Stack Overflow用户
提问于 2013-10-09 05:59:02
回答 1查看 480关注 0票数 5

现在我正在使用HTTPRequest,一切都很好。但我想处理以下情况

Device is connected to wifi but wifi is not connected to internet

我使用的是对点时间,但老实说,它需要自己的时间来超时,这是40秒,不少于这一点。请求代码如下:

代码语言:javascript
复制
  public String[] doHttpGetWithCode_1(String url, String headerParam) throws Exception {


         weburl         = new URL(url);
         uri            = new URI(weburl.getProtocol(), weburl.getUserInfo(), weburl.getHost(), weburl.getPort(), weburl.getPath(), weburl.getQuery(), weburl.getRef());
         result         = new String[2];
         httpParameters = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is established.
         timeoutConnection = 3000;
         HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            // Set the default socket timeout (SO_TIMEOUT) 
            // in milliseconds which is the timeout for waiting for data.
        timeoutSocket   = 3000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        httpURL=(HttpURLConnection) weburl.openConnection();
        httpURL.setDoOutput(true);
        httpURL.setDoInput(true);
        httpURL.setRequestMethod("GET");
        client          =new  DefaultHttpClient(httpParameters);  
        httpget = new HttpGet(url);
        httpget.addHeader("Content-Type",   "application/json");
        httpget.addHeader("Authorization", "Basic " + Base64.encodeToString(("greggs:sausage123").getBytes(),Base64.NO_WRAP));
        httpget.addHeader("X-Auth-Signature",   headerParam);
        httpget.setParams(httpParameters);

        try{
            response    = client.execute(httpget);
            entity      = response.getEntity();
            is          = entity.getContent();
//          BufferedReader br = new BufferedReader(new InputStreamReader(in );
            reader      = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8));
            sb          = new StringBuilder();
            line        = null;

                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
        }
        catch(ClientProtocolException CPE)
        {
            CPE.printStackTrace();
        }


            result[0] = response.getStatusLine().getStatusCode()+"";
            result[1] = sb.toString();
            return result;
        }

在运行突出显示的场景后,我得到了以下响应:

代码语言:javascript
复制
10-09 10:42:00.845: W/System.err(6052): java.net.UnknownHostException: Unable to resolve host "xxx.xxx.com": No address associated with hostname
    10-09 10:42:00.850: W/System.err(6052):     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
    10-09 10:42:00.850: W/System.err(6052):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    10-09 10:42:00.855: W/System.err(6052):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
    10-09 10:42:00.855: W/System.err(6052):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
    10-09 10:42:00.855: W/System.err(6052):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    10-09 10:42:00.860: W/System.err(6052):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    10-09 10:42:00.860: W/System.err(6052):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    10-09 10:42:00.860: W/System.err(6052):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
    10-09 10:42:00.860: W/System.err(6052):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
    10-09 10:42:00.865: W/System.err(6052):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    10-09 10:42:00.870: W/System.err(6052):     at com.putitout.Utils.Network.doHttpGetWithCode_1(Network.java:363)
    10-09 10:42:00.875: W/System.err(6052):     at com.putitout.GREEGS.Rewards.RewardHomeScreen$18.run(RewardHomeScreen.java:1157)
    10-09 10:42:00.880: W/System.err(6052): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
    10-09 10:42:00.880: W/System.err(6052):     at libcore.io.Posix.getaddrinfo(Native Method)
    10-09 10:42:00.885: W/System.err(6052):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
    10-09 10:42:00.885: W/System.err(6052):     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
    10-09 10:42:00.885: W/System.err(6052):     ... 11 more
    10-09 10:42:00.890: W/System.err(6052): Caused by: libcore.io.ErrnoException: getaddrinfo failed: ETIMEDOUT (Connection timed out)
    10-09 10:42:00.890: W/System.err(6052):     ... 14 more

现在,我需要做的是添加计时器,以特定的时间间隔运行,需要一点帮助,在此之前,谢谢您的注意。

EN

回答 1

Stack Overflow用户

发布于 2014-03-26 17:23:56

只需将权限添加到清单中即可

代码语言:javascript
复制
<uses-permission android:name="android.permission.INTERNET" />
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19264248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档