1

我想向用户显示超时消息,而不是强制关闭应用程序

这是我的代码

String xml = null;

BasicHttpParams params = new BasicHttpParams();
ConnManagerParams.setTimeout(params, 1000);
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);

// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient(params);
HttpPost httpPost = new HttpPost(url);

try {
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
    //e.printStackTrace();
} catch (ClientProtocolException e) {
    //e.printStackTrace();
} catch (IOException e) {
    //e.printStackTrace();
}finally{
    httpClient.getConnectionManager().shutdown();
}
return xml;

如果连接超时应用程序强制关闭并给我这个错误:

  org.apache.http.conn.ConnectTimeoutException: Connect to /xx.xx.xx.xx:xx timed out
4

0 回答 0