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

In my application, I connect to a server that responds with xml. I parse it with SAX Parser and get the data.

The question arises:

What is the best way to solve connection problems?

(At the moment, if the Internet connection is not available, the application simply continues to show ProgressDialog i)

+4
source share
2 answers

As with the Heiko Rupp offer, you can also check for network connectivity before downloading. See my related post .

0
source

So you basically do (Pseudocode)

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

In this case, wrap the parsing material and cancel the "Exception" dialog

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

You can also do try { .. } catch (XYZException e ; pd.cancel(); throw e) if you want to handle Exception at a different level in your application.

+4
source

Source: https://habr.com/ru/post/1337106/


All Articles