You can put all this in a while :
while (true) { try { ... } catch (IOException e) { continue; } return buffer.toString(); }
The return will take you out of the loop. You can also track the number of attempts and stop after 5-10, for courtesy, but this is the main form.
Edit
Best comment based version:
int retries = 10; for (int i = 0 ; i < retries ; i++) { try { ... } catch (IOException e) { continue; } return buffer.toString(); }
andronikus
source share