2010-09-27 93 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; 
的[示例代碼來處理例外]