【问题标题】:HttpResponse & Timeout for AndroidAndroid 的 HttpResponse 和超时
【发布时间】:2012-10-26 13:17:21
【问题描述】:

我希望我的设备在 5 秒后放弃 http 连接。 但是我的代码不起作用......关闭网络时我从未收到任何超时消息。 就像设备仍然尝试连接一样,尽管超时...

有想法吗? 我是否试图捕捉正确的异常?

谢谢。

        try 
        {
            HttpClient httpclient = new DefaultHttpClient();       
                HttpPost httppost = new HttpPost(URL);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

                HttpResponse response = httpclient.execute(httppost);

                if (response.getStatusLine().getStatusCode() < 400) 
                {
                        ... //data processing
                } 
                else 
                {
                    errorMsgId = R.string.http_site_error;                        
                }
        } 
        catch (ConnectTimeoutException e)
        {
            Toast.makeText(this, "Network timeout reached!", Toast.LENGTH_SHORT).show();
            Log.e("+++++++++++++++++ ","Network timeout reached!"); 
        }

【问题讨论】:

    标签: java android timeout httpresponse


    【解决方案1】:

    好的,知道了,所以如果这可以帮助其他人:

                        HttpClient httpclient = new DefaultHttpClient();
                        final HttpParams httpParams = httpclient.getParams();
                        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
                        HttpConnectionParams.setSoTimeout(httpParams, 5000);
                        HttpPost httppost = new HttpPost(URL);
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
                        HttpResponse response = httpclient.execute(httppost);
    

    【讨论】:

      【解决方案2】:

      你可以这样使用:

      /**
       * Check availability of web service
       * 
       * @param host Address of host
       * @param seconds Timeout in seconds
       * @return Availability of host
       */
      public static boolean checkIfURLExists(String host, int seconds)
      {
          HttpURLConnection httpUrlConn;
          try
          {
              httpUrlConn = (HttpURLConnection) new URL(host).openConnection();
      
              // Set timeouts in milliseconds
              httpUrlConn.setConnectTimeout(seconds * 1000);
              httpUrlConn.setReadTimeout(seconds * 1000);
      
              // Print HTTP status code/message for your information.
              System.out.println("Response Code: " + httpUrlConn.getResponseCode());
              System.out.println("Response Message: "
                      + httpUrlConn.getResponseMessage());
      
              return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
          }
          catch (Exception e)
          {
              System.out.println("Error: " + e.getMessage());
              return false;
          }
      }
      

      【讨论】:

      • 很好,是的......但如果可能的话,我最好在我的 HttpResponse 连接命令中包含一个超时,而不是进行两次连接,因为我很确定我的老板会注意到它并且不会喜欢它:(
      【解决方案3】:

      也许我遗漏了一些东西,但是您在哪里将设置超时的参数与您创建的 HttpClient 相关联?你不应该这样做吗:

      HttpParams httpParameters = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
      HttpConnectionParams.setSoTimeout(httpParameters, 5000);
      ...
      HttpClient httpclient = new DefaultHttpClient(httpParameters);
      

      【讨论】:

      • 这也是我在找到这些示例时所想的,但似乎是这样......
      猜你喜欢
      • 2015-08-24
      • 2010-10-16
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多