0

我正在使用 url.openstream 向服务器请求。如果在此期间没有互联网连接,我希望将数据存储在数据库中,因此在 IOException 的 catch 子句中进行存储,但它不会被捕获,它只是挂在 url.openstream 上。我什至等了一分钟,但它仍然没有被 IOException catch 子句抓住。

我必须做些什么来克服这个问题?

4

1 回答 1

2

通过使用以下方法解决了问题,而不是使用 url.openstream。

    public HttpResponse getResp(String request) throws IOException
     {


    HttpGet httpGet = new HttpGet(request);
    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 = 30000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 40000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpResponse response = httpClient.execute(httpGet);
    return response;
}
于 2013-02-06T08:19:43.310 回答