2013-07-08 38 views
0

我試圖從craigslist位置頁面讀取源代碼。有時我得到一個異常,不明白爲什麼。這是奇怪的,通過我的手機3g它會工作一段時間,然後從沒有開始拋出異常的地方。當我在WiFi上測試它時,它會在75%的時間內拋出異常。閱讀源代碼時出現Java android異常


07-08 02:43:39.542: W/System.err(16898): java.net.SocketTimeoutException 
07-08 02:43:39.562: W/System.err(16898): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:461) 
07-08 02:43:39.562: W/System.err(16898): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:85) 
07-08 02:43:39.566: W/System.err(16898): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103) 
07-08 02:43:39.570: W/System.err(16898): at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134) 
07-08 02:43:39.570: W/System.err(16898): at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:161) 
07-08 02:43:39.574: W/System.err(16898): at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159) 
07-08 02:43:39.574: W/System.err(16898): at java.io.InputStreamReader.read(InputStreamReader.java:255) 
07-08 02:43:39.574: W/System.err(16898): at java.io.BufferedReader.fillBuf(BufferedReader.java:128) 
07-08 02:43:39.578: W/System.err(16898): at java.io.BufferedReader.readLine(BufferedReader.java:396) 
07-08 02:43:39.578: W/System.err(16898): at com.codalata.craigslistchecker.GetLocation.GetLocationData(GetLocation.java:56) 
07-08 02:43:39.578: W/System.err(16898): at com.codalata.craigslistchecker.Location$1.run(Location.java:407) 
07-08 02:43:39.582: W/System.err(16898): at java.lang.Thread.run(Thread.java:1019) 

我只是試圖從服務器讀取。任何幫助,將不勝感激!

我把計數器放在while循環中,看看它在錯誤發生前有多少次循環。有時會循環519次,其他循環會循環400次。

public class GetLocation { 
int CityindexEnd; 
int CityindexStart; 
int StateC; 
String URLME = null; 
String Search = "a href=\""; 
int count = 0; 

@SuppressWarnings("finally") 
public String GetLocationData() 
     throws Exception { 
    BufferedReader in = null; 
    String data = null; 
    try { 
     HttpGet httpGet = new HttpGet(
       "http://www.craigslist.org/about/sites"); 
     HttpParams httpParameters = new BasicHttpParams(); 
     int timeoutConnection = 6000; 
     HttpConnectionParams.setConnectionTimeout(httpParameters, 
       timeoutConnection); 
     int timeoutSocket = 12000; 
     HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
     DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
     HttpResponse response = httpClient.execute(httpGet); 
     in = new BufferedReader(new InputStreamReader(response.getEntity() 
       .getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String l = ""; 
     String ln = System.getProperty("line.separator"); 
     while ((l = in.readLine()) != null) { 
      count++; 
      sb.append(l + ln); 
     } 
     in.close(); 
     data = sb.toString(); 
        return data; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
       return URLME; 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     return URLME; 
    } 
} 
+0

顯然長時間沒有服務器返回或您的互聯網連接速度很慢,您是否通過在Web瀏覽器上運行它來嘗試請求? –

+0

我在我的瀏覽器上加載它就好了。如果您將url更改爲http://www.yahoo.com/,它可以正常工作。 – Xjasz

+0

您是否嘗試更改超時時間? – Marek

回答

0

你設置超時與

int timeoutConnection = 4000; 
HttpConnectionParams.setConnectionTimeout(httpParameters, 
     timeoutConnection); 
int timeoutSocket = 7000; 
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

基本上,你失去的連接(或者從來沒有完全擁有它),並正在採取太長。如果你不想超時,你可以刪除這些行。如果你這樣做,你需要優雅地處理這種例外。

+0

Web瀏覽器正在加載頁面。另外,如果我在http://houston.craigslist.org/中爲它的url讀取它很好。我把超時設置爲告訴它只是給它,否則它只是坐在那裏無所事事。我的互聯網速度非常快。 – Xjasz

+0

Web瀏覽器的超時時間更長,如果有的話。你正在設置4秒和7秒,那些非常小。無論如何,如果你指定一個超時,你必須處理這個可能性,否則你可能會隨機崩潰。 –

+0

超時已被刪除,所以現在它只是試圖從頁面讀取。如果我更改了網址,它會顯示良好。 – Xjasz