I’m using the DefaultHTTPClient in Android to fetch pages. I’d like to trap 500 and 404 errors returned by the server but all I get is a java.io.IOException. How can I specifically trap those two errors?
Here’s my code:
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;
}
You need to get
statusCode