Android Force App Disabled If No Internet Connection

When there is no internet connection, my application is down, and when the user closes the internet connection while the application is still running, it exits.

protected void onHandleIntent(Intent intent)
{

    Log.d(Constants.TAG, "Service started");

    List<RssItem> rssItems = null ;
    try {
        PcWorldRssParser parser = new PcWorldRssParser();
        if(flag == 0) {
            rssItems = parser.parse(getInputStream(RSS_LINK));
        }
        else if(flag == 2){
            rssItems = parser.parse(getInputStream(RSS_LINK2));
        }
        else if(flag == 3){
            rssItems = parser.parse(getInputStream(RSS_LINK3));
        }
        else if(flag == 4){
            rssItems = parser.parse(getInputStream(RSS_LINK4));
        }
        else if(flag == 5){
            rssItems = parser.parse(getInputStream(RSS_LINK5));
        }
        else if(flag == 6){
            rssItems = parser.parse(getInputStream(RSS_LINK6));
        }
        else if(flag == 7){
            rssItems = parser.parse(getInputStream(RSS_LINK7));
        }
        else if(flag == 8){
            rssItems = parser.parse(getInputStream(RSS_LINK8));
        }
        else if(flag == 9){
            rssItems = parser.parse(getInputStream(RSS_LINK9));
        }
    } catch (XmlPullParserException e) {
        Log.w(e.getMessage(), e);
    } catch (IOException e) {
        Log.w(e.getMessage(), e);
    }
    Bundle bundle = new Bundle();
    bundle.putSerializable(ITEMS, (Serializable) rssItems);
    ResultReceiver receiver = intent.getParcelableExtra(RECEIVER);
    receiver.send(0, bundle);
}

      

RssParser class:

public List<RssItem> parse(InputStream inputStream) throws XmlPullParserException, IOException {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(inputStream, null);
        parser.nextTag();
        return readFeed(parser);

    } finally {

         inputStream.close();

    }
}

      

In these two methods, I get a NullPointerException

at:

inputStream.close();
rssItems = parser.parse(getInputStream(RSS_LINK));

      

+3


source to share


3 answers


You can put this in all of your else ifs

try {
               rssItems = parser.parse(getInputStream(RSS_LINK));
           }catch (XmlPullParserException e){
               throw new RuntimeException("Error"+ e.getMessage());
           }catch (IOException e){
               throw new RuntimeException("Error"+ e.getMessage());
           }catch (NullPointerException e){
               Log.d("Error in RSS_LINK","");
           }

      



You need the above code to handle all possible exceptions correctly.

Also you should change your code if else if

to switch case

, since you have many else ifs

with constant checks, it would be easier to read and faster to run if you convert them toswitch case

+1


source


You can create a method to check whether the internet connection is available or not,

private boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    boolean isAvailable = false;
    if (networkInfo != null && networkInfo.isConnected()) {
        isAvailable = true;
    }
    return isAvailable;
}

      

If the Internet is available,



if (isNetworkAvailable()) {
            // Do your stuff here.
        }
else {
}

      

Tip : do your networking work in the AsycnTask class .

+1


source


Your application ends because you are not handling the exception.

Now you can check your internet connection right after opening the application, but this is not the correct solution to your problem, because you may lose the internet connection between them and the same exception will be thrown again.

So, you have to handle the exception.

0


source







All Articles