2010-11-25 192 views
2

如果在特定時間段內沒有服務器響應,是否有任何方法可以在android中設置超時時間。如何在android中設置超時時間

+0

你能更具體嗎?當然,你們使用哪種課程來滿足你的要求? – Tobias 2010-11-25 06:46:39

回答

1

下面是代碼,我用的時間不

uri = new URI(url); 
      HttpGet method = new HttpGet(uri); 
      method.addHeader("Content-Type", "application/json"); 
      HttpParams httpParameters = new BasicHttpParams(); 
      int timeoutConnection = 60000; 
      HttpConnectionParams.setConnectionTimeout(httpParameters, 
        timeoutConnection); 
      int timeoutSocket = 65000; 
      HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
      DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
      HttpResponse response = httpClient.execute(method); 
      HttpEntity responseEntity = response.getEntity(); 
      statuscode = response.getStatusLine().getStatusCode(); 

在try catch塊將這個,如果超過時間就會拋出異常

+0

謝謝,我只看這與此有關.. – Zoombie 2010-11-28 15:48:10

+0

請檢查以下鏈接:http://stackoverflow.com/questions/693997/how-to-set-httpresponse-timeout-for-android-in-java – Zoombie 2011-02-21 11:52:21

0

之前嘗試檢查coonectivity到url第一尋求數據回覆