-1

in my app I have to "download" a string from a file and when user disables internet and the app is open with AsyncTask operation it crashes because the internet operation doesn't work and the error flows in IOException catch. I tried to do an operation in catch(IOException e) but it seem to doesn't work because app crash anyway. How can I avoid my app to crash? Code:

try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(MegaMethods.url + MegaMethods.BuildSelectedPath + "_Link.txt").openStream()));
        String line = reader.readLine();
        for (int k = 0; k < params[0] ; k++) {
            line = reader.readLine();
        }
        return line;
    }catch (IOException e){
        e.printStackTrace();
        Log.i("IOEXCEPTION", "");
        MegaMethods Errorr = new MegaMethods();
        Errorr.Back();
        Errorr.Error();
    }

Logcat

01-06 23:07:59.389  14512-14512/sparkyka.it.pcbuilds E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{sparkyka.it.pcbuilds/sparkyka.it.pcbuilds.Office}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
        at android.app.ActivityThread.access$1500(ActivityThread.java:117)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)

I don't want to check internet connection, if internet operation doesn't work correctly I want to avoid app to crash and do stuff

ClearCode
  • 47
  • 4
  • 11

2 Answers2

0

Check for the internet connection before making the request like this

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
         = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

One drawback of code above is that when you are on wifi but that wifi doesn't have internet connection; in that case also it would return true. So another option is this

public static boolean isInternetAccessible(Context context) {
if (isNetworkAvailable()) {
    try {
        HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
        urlc.setRequestProperty("User-Agent", "Test");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1500); 
        urlc.connect();
        return (urlc.getResponseCode() == 200);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Couldn't check internet connection", e);
    }
} else {
    Log.d(LOG_TAG, "Internet not available!");
}
return false;
}

If network call is already in progress and internet goes, then above code won't work. You need to handle that manually in code. Use a try-catch block.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

for me it's solved by use general try and catch. means you have to use

try { 

    // do something

} catch (Exception e) {

// e.printStackTrace();

}

instead of just

    catch (IOException e){

}
Amir Poursadegh
  • 101
  • 1
  • 9