我正在使用DefaultHTTPClient
Android 中的来获取页面。我想捕获服务器返回的 500 和 404 错误,但我得到的只是java.io.IOException
. 我怎样才能专门捕获这两个错误?
这是我的代码:
public String doGet(String strUrl, List<NameValuePair> lstParams) throws Exception {
Integer intTry = 0;
while (intTry < 3) {
intTry += 1;
try {
String strResponse = null;
HttpGet htpGet = new HttpGet(strUrl);
DefaultHttpClient dhcClient = new DefaultHttpClient();
dhcClient.addResponseInterceptor(new MakeCacheable(), 0);
HttpResponse resResponse = dhcClient.execute(htpGet);
strResponse = EntityUtils.toString(resResponse.getEntity());
return strResponse;
} catch (Exception e) {
if (intTry < 3) {
Log.v("generics.Indexer", String.format("Attempt #%d", intTry));
} else {
throw e;
}
}
}
return null;
}