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,以用於日誌記錄目的。這需要額外的時間,直到實際發生異常。