我正在使用url.openstream来请求服务器。如果在此期间没有互联网连接,我希望数据存储在数据库中,因此在IOException的catch子句中进行存储,但它不是在这里被捕获,而是挂在url.openstream上。 我甚至等了一会儿但是,它仍然没有被IOException catch子句捕获。
我该怎么做才能克服这个问题?
答案 0 :(得分:1)
使用以下方法解决了问题,而不是使用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;
}