I am making an http request. I am on a platform (Android) where network operations often fail because network connection may not be available. Therefore, I would like to try the same connection N times before completely failing. Thought of something like this:
DefaultHttpClient mHttp = ...; public HttpResponse runHttpRequest(HttpRequestBase httpRequest) throws IOException { IOException last = null; for (int attempt = 0; attempt < 3; attempt++) { try { HttpResponse response = mHttpClient.execute(httpRequest); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { return response; } } catch (IOException e) { httpRequest.abort(); last = e; } } throw last; }
I am mostly worried that the connection is in some state, which is not valid on subsequent attempts. In other words, I need to completely recreate 'httpRequest', should I avoid calling httpRequest.abort () in the catch block and only call it on the last crash?
thanks
java android
user291701
source share