Search code examples
androidandroid-internet

android app force terminate when no internet connection


When there is no internet connection, my application is not running and when the user closes the internet connection while the application is still running it then terminates.

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 this two methods I get a NullPointerException at:

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

Solution

  • You can put this in all 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 in order to handle all possible exceptions properly.

    Also you should change your if else if code to a switch case since you have many else ifs with constant checks it would be simpler to read and faster to run if you transform them to switch case