I would be happy to hear feedback on my implementation of HttpClient. I am not so strong with error handling so I want to improve. My thought process behind this was that IOException
s can be recoverable and should be tried again. Where as client exception isn't. Hope to hear your thoughts.
private HttpResponse execute(int httpVerb, boolean authHeader) throws NoInternetConnectionError, IOException, ClientProtocolException {
try {
Log.i(TAG, "setting header and executing");
switch (httpVerb) {
case HTTP_POST:
if (authHeader){
httpPost.addHeader(header);
}
return httpClient.execute(httpPost);
case HTTP_PUT:
if (authHeader){
httpPut.addHeader(header);
}
return httpClient.execute(httpPut);
case HTTP_GET:
if (authHeader){
httpGet.addHeader(header);
}
return httpClient.execute(httpGet);
default:
return null;
}
} catch (IOException e) {
InternetChecker internetChecker = new InternetChecker(context);
if (internetChecker.isInternetConnected() == false) {
throw new NoInternetConnectionError("internet not connected");
} else {
numberOfTries ++;
if (numberOfTries > 2) {
throw new IOException(e.getMessage());
} else {
Log.i(TAG, "executing for the " + String.valueOf(numberOfTries) + " time");
return execute(httpVerb, authHeader);
}
}
} finally {
numberOfTries = 0;
Log.i(TAG, "finally block run");
}
}