Abramodj
Abramodj

Reputation: 5879

Android - Downloading data from the internet >> catch connection errors

In my app i connect to a server, which responds with an xml. I parse it with the SAX Parser, and get the data.

The question is:

What is the best way to handle connection issues?

(At this moment if there is no internet connection available the app simply continues showing the ProgressDialog i implemented)

Upvotes: 4

Views: 1245

Answers (2)

dave.c
dave.c

Reputation: 10908

As well as following the suggestion of Heiko Rupp, you can also check for the availability of a network connection prior to performing your download. See my post on the subject.

Upvotes: 0

Heiko Rupp
Heiko Rupp

Reputation: 30944

So you basically do (Pseudo code)

ProgessDialog pd = new ProgressDialog(this).show();
Sax.parseStuff();
pd.dismiss();

In this case, wrap the parsing stuff and cancel the dialog on Exception

ProgessDialog pd = new ProgressDialog(this).show();
try {
   Sax.parseStuff();
}
finally {
   pd.dismiss();   // or cancel
}

You can also do a try { .. } catch (XYZException e ; pd.cancel(); throw e) if you want to process the Exception in a different layer of your app.

Upvotes: 4

Related Questions