更新:Android HttpGet:在執行請求之前應該檢查哪些異常?
String xmlList = null;
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
HttpGet request = new HttpGet(URL);
HttpResponse response = null;
response = httpClient.execute(request);
//HttpResponse httpResponse = httpClient.execute(request);
HttpEntity httpEntity = response.getEntity();
xmlList = EntityUtils.toString(httpEntity);
}
catch (MalformedURLException e) {
xmlList = "URL: is a malformed URL";
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
xmlList = "URL: UnsupportedEncodingException";
} catch (ClientProtocolException e) {
e.printStackTrace();
xmlList = "URL: ClientProtocolException";
} catch (SocketTimeoutException e) {
e.printStackTrace();
xmlList = "URL: SocketTimeoutException";
} catch (ConnectTimeoutException e) {
e.printStackTrace();
xmlList = "URL: ConnectTimeoutException";
} catch (IOException e) {
xmlList = "URL: IOException";
e.printStackTrace();
}
finally {
httpClient.getConnectionManager().shutdown();
}
// return XML
return xmlList;
更新的最終
我建立一個應用程序,從網站上讀取數據,我想知道什麼是例外前應檢查執行請求?
我想到的例外:
1) bad url
2) host not responding
3) time out
....
....
這裏是我的代碼:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(SongsManager1.URL);
HttpResponse response = null;
response = httpClient.execute(request);
//HttpResponse httpResponse = httpClient.execute(request);
HttpEntity httpEntity = response.getEntity();
xmlList = EntityUtils.toString(httpEntity);
如何在發出請求之前檢查主機是否未響應(與超時有何不同)? – 2012-03-19 00:50:28
你的主機沒有響應和超時是一樣的,沒想到:) – 2012-03-19 01:10:26