0

In my application I am sending multiple http requests. Since, the network may go down at any time. So, before sending each request should I use android ConnectionManager to check the network state? I am feeling that some other better way would be there to handler this.(Rather than using the android connection manager service each time when multiple request are sent on each fragment)

Rahul Rastogi
  • 4,486
  • 5
  • 32
  • 51

1 Answers1

2

Take a look at this question. If you want to avoid use of the ConnectionManager, you could use a try-catch block and check for an exception while trying to make a connection (provided you are using the java.net.* library to do the HTTP requests). For example, something like this was used in that question:

URL url = ...

HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setReadTimeout(15000);

try {
    InputStream in = new BufferedInputStream(c.getInputStream());
    httpResult = readStream(in);
} catch (IOException e) {

    // Here is where an exception would be thrown if there is no internet.
    // Would likely actually be an UnknownHostException

    Log.e(TAG, "Error: ", e);

} finally {
    c.disconnect();
}
Community
  • 1
  • 1
Joel
  • 4,732
  • 9
  • 39
  • 54