2010-09-27 45 views
0

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

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

请提供相同的代码。

+0

可能重复(http://stackoverflow.com/questions/3805855/sample-code-to-handle-exceptions) – 2010-09-27 17:21:12

+0

请不要” t多次发布相同的问题。如果您想向问题添加其他信息,则应该对其进行编辑或添加评论。 – 2010-09-27 17:21:46

回答

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; 
的[示例代码来处理例外]