简体   繁体   中英

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));

You can create a method to check if 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 Internet is available,

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

Advice : perform your network related operation in AsycnTask class.

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

Your app is terminating because you are not handling the exception.

Now, you can check for an internet connection right after opening the app, but that is not the proper solution to your problem because you could lose internet connectivity in between and the same exception would be caused again.

So, you must handle the exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM