为HttpConnection处理的异常

时间:2010-09-27 17:00:37

标签: android

请提供处理 HttpConnection或相关例外&的处理代码。如何向用户显示。

具体来说,我想知道如何处理 TimeOut for HttpConnection&如何向用户显示该警报。

请提供相同的代码。

1 个答案:

答案 0 :(得分:2)

这是代码

HttpPost hPost = new HttpPost(url);
StringEntity se = new StringEntity(envelope,HTTP.UTF_8);
hPost.setEntity(se);

HttpParams httpParameters = new BasicHttpParams();

// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 3000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(hPost);

HttpEntity entity = httpResponse.getEntity();
return entity;