I have an app that uses JSoup to connect to a web server, and it works fine. Unfortunately, the said web server isn’t very reliable. I get SocketException because of time-out connection quite often. I make the connection in a modified IntentService, and I just simply repeat onHandleIntent(intent) in the catch(Exception e) block.
catch(Exception e){
Log.d(Tag, "in catch Exception block...");
onHandleIntent(intent);
}
Theoretically, this should work. But sometimes, I get stack over flow error, and the app ended quite ungracefully. So, what can I do to make it better?
I want to continue to call onHandleIntent, so, maybe I have to call it in iteration instead of recursively. If you can give me advice on how to implement this iteratively, it would be very helpful. Thanks!
That is correct. If you handle this recursively, a server that continually times out will inevitably result in a stack overflow.
Something like this:
(Maybe someone can think of a less “clunky” way to write this …)