0

我正在尝试从 craigslist 位置页面读取源代码。有时我会遇到异常,但不明白为什么。通过我的手机 3g 很奇怪,它会工作一段时间然后突然开始抛出异常。当我在我的 WiFi 上测试它时,它会在 75% 的时间里抛出这个异常。


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;
    }
}
4

1 回答 1

0

您正在设置超时

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

基本上,您正在失去连接(或从未完全连接)并且花费的时间太长。如果您不想超时,可以删除这些行。如果这样做,则需要优雅地处理该异常。

于 2013-07-08T07:03:04.380 回答