避免应用程序崩溃与IOException异常或互联网缺失 [英] Avoid app crash with IOException or internet is missing

查看:170
本文介绍了避免应用程序崩溃与IOException异常或互联网缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有下载从文件中的字符串,当用户禁用网络和应用程序是AsyncTask的操作打开它崩溃,因为网络操作不工作,在IOException异常捕获错误流。我试图赶上(IOException异常E)做一次手术,但它似乎并没有因为程序崩溃工作反正。如何避免我的应用程序崩溃? code:

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的

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

推荐答案

检查作出这样的请求之前的互联网连接。

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

code以上的一个缺点是,当你在WiFi但无线没有互联网连接;在这种情况下也将返回true。因此,另一种选择是这样的。

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

如果网络呼叫已在进行中和互联网去,然后上面code将无法工作。您需要手动的处理code。使用try-catch块。

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.

这篇关于避免应用程序崩溃与IOException异常或互联网缺失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆