1

所以我遇到了一个不工作的套接字超时。我遵循现有职位给出的所有指示,但它仍然不工作(我永远不会得到套接字超时异常)。这里是我的代码:Android - setSoTimeout不起作用

AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() { 
    @Override 
    protected String doInBackground(String... params) { 
     String location = params[0]; 

     try { 
     HttpGet httpGet = new HttpGet(location); 
     HttpParams httpParameters = new BasicHttpParams(); 

     // Set the timeout in milliseconds until a connection is established. 
     // The default value is zero, that means the timeout is not used. 
     int timeoutConnection = 0; 
     HttpConnectionParams.setConnectionTimeout(httpParameters, 
       timeoutConnection); 

     // Set the default socket timeout (SO_TIMEOUT) 
     // in milliseconds which is the timeout for waiting for data. 
     int timeoutSocket = 2000; 
     HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

     DefaultHttpClient client = new DefaultHttpClient(httpParameters); 
     Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! "); 
     HttpResponse response = client.execute(httpGet); 
     Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! "); 

     if (statusLine.getStatusCode() == HttpStatus.SC_OK) { 
      ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      response.getEntity().writeTo(out); 
      return new Scanner(out.toString()).useDelimiter("\\A").next(); 
     } else { 
      return ""; 
     } 
     } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
     } catch (URISyntaxException e) { 
     e.printStackTrace(); 
     return null; 
     } 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     try { 
     // doStuff 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 
}; 
task.execute(location); 

所以我应该在两秒后得到一个套接字超时异常,但客户端只是忽略了这一点。任何帮助?

在此先感谢


因此,这里是所有的代码:

public void fetch(String location) {  
    AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() { 
     @Override 
     protected String doInBackground(String... params) { 
      String location = params[0]; 

      try { 
       HttpGet httpGet = new HttpGet(location); 
       HttpParams httpParameters = new BasicHttpParams(); 
       // Set the timeout in milliseconds until a connection is established. 
       // The default value is zero, that means the timeout is not used. 
       int timeoutConnection = 0; 
       HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
       // Set the default socket timeout (SO_TIMEOUT) 
       // in milliseconds which is the timeout for waiting for data. 
       int timeoutSocket = 2000; 
       HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

       DefaultHttpClient client = new DefaultHttpClient(httpParameters); 
       Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! "); 
       HttpResponse response = client.execute(httpGet); 
       Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! "); 

       if (statusLine.getStatusCode() == HttpStatus.SC_OK) { 
        ByteArrayOutputStream out = new ByteArrayOutputStream(); 
        response.getEntity().writeTo(out); 
        return new Scanner(out.toString()).useDelimiter("\\A").next(); 
       } else { 
        return ""; 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } catch (URISyntaxException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      try { 
       //doStuff 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 
    task.execute(location); 
} 

回答

0

在我的例如两个超时设置。连接超时抛出“java.net.SocketTimeoutException:套接字未连接”,套接字超时“java.net.SocketTimeoutException:操作超时”。

HttpGet httpGet = new HttpGet(url); 
HttpParams httpParameters = new BasicHttpParams(); 
// Set the timeout in milliseconds until a connection is established. 
// The default value is zero, that means the timeout is not used. 
int timeoutConnection = 3000; 
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data. 
int timeoutSocket = 5000; 
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
HttpResponse response = httpClient.execute(httpGet); 

如果你想设置的任何现有了HTTPClient(例如DefaultHttpClient或AndroidHttpClient)的参数,你可以使用函数setParams()获取。

httpClient.setParams(httpParameters);

+1

没有什么改变...我已经尝试过 – user1416721

+0

hi @ user1416721我已经添加了新的代码。它为我工作。我前两天使用过。工作得很好。使用它并让我知道。 – itsrajesh4uguys

+1

不,还不行。客户端开始执行并且不会在5秒内完成,并且不会引发SocketTimeoutException ...也许它有一些要做的事情,我在doInBackground中将它用作AsyncTask? – user1416721

2

后来我有类似的问题。我已经尝试了一下,注意到当我在模拟器中运行我的应用程序时,超时无效,当我在真实设备上运行它时,它确实有效。我用DefaultHttpClientHttpUrlConnectionAndroidHttpClient进行了测试,所有三项都显示了相同的结果;一个IOexception(UnknownHostException)在模拟器中约20秒后,不管任何设置超时。

谷歌搜索发现,其他人也报告了超时问题:

所提出的解决方案中没有工作对我来说,所以我猜只有reli能够解决的办法是自己管理超时。

0

参见:https://stackoverflow.com/a/20031077/2609238

问题可能是在Apache HTTP客户端。请参阅HTTPCLIENT-1098。在4.1.2中修复。

超时异常会尝试将DNS反向IP,以用于日志记录目的。这需要额外的时间,直到实际发生异常。