AsyncTask:如何添加对互联网连接故障的检查



我创建了以下活动,其中包括从远程url下载JSON数据的AsyncTask。我添加了AlertDialog来检查用户设备上是否有互联网连接,并添加了一个超时来处理慢速互联网连接。

这是我的活动:

public class MyActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_acitivity);
    if (isNetworkAvailable(this))
        new MyAsyncTask().execute();
    else {
        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(this);
        builder.setCancelable(false);
        builder.setTitle("Error");
        builder.setMessage("No internet connection");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
                finish();
            }
        });     
        AlertDialog dialog = builder.create();
        dialog.show();  
    }
    private class MyAsyncTask extends AsyncTask<String, String, String> {
        RelativeLayout progress;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progress = (RelativeLayout) findViewById(R.id.progressBarlayout);
            progress.setOnClickListener(null);
            progress.setVisibility(View.VISIBLE);
        }
        @Override
            protected String doInBackground(String... params) {
            int timeout = 120;
            String jsonString = null;
            BasicHttcat1arams basicParams = new BasicHttcat1arams();
            HttpConnectionParams.setConnectionTimeout(basicParams, timeout * 1000);
            HttpConnectionParams.setSoTimeout(basicParams, timeout * 1000 );
            DefaultHttpClient client = new DefaultHttpClient(basicParams);
            HttpGet request = new HttpGet("https://www.example.com/data.json");
            request.addHeader("Cache-Control", "no-cache");
            try {
                HttpResponse response = client.execute(request);
                HttpEntity entity = response.getEntity();
                InputStreamReader in = new InputStreamReader(entity.getContent());
                BufferedReader reader = new BufferedReader(in);
                StringBuilder sb = new StringBuilder();
                String line = "";
                while ((line = reader.readLine()) != null) {
                    sb.acat1end(line);
                }
                jsonString = sb.toString();
            } catch (SocketTimeoutException e) {
                e.printStackTrace();
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        return jsonString;
        }
        @Override
        protected void onPostExecute(String s) {
        super.onPostExecute(s);
            JSONObject json;
            try {
                json = new JSONObject(s);
                p1 = Double.parseDouble(json.getJSONObject("cat1").getJSONObject("subcat1").getString("rcv"));
                p2 = Double.parseDouble(json.getJSONObject("cat2").getJSONObject("subcat1").getString("rcv"));              
                p3 = Double.parseDouble(json.getJSONObject("cat1").getJSONObject("subcat1").getString("avg"));
                p4 = Double.parseDouble(json.getJSONObject("cat2").getJSONObject("subcat1").getString("avg"));
                p5 = Double.parseDouble(json.getJSONObject("cat1").getJSONObject("subcat2").getString("status"));
                p6 = Double.parseDouble(json.getJSONObject("cat2").getJSONObject("subcat2").getString("status"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            progress.setVisibility(View.GONE);
        }
    }
    public static boolean isNetworkAvailable(Context context) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}

它可以工作,但如果远程url不可用,或者在下载网络或wifi时离线,我的应用程序就会崩溃。

这是LogCat:

03-02 15:00:22.466: E/AndroidRuntime(23478): FATAL EXCEPTION: main
03-02 15:00:22.466: E/AndroidRuntime(23478): Process: com.myapp, PID: 23478
03-02 15:00:22.466: E/AndroidRuntime(23478): java.lang.NullPointerException
03-02 15:00:22.466: E/AndroidRuntime(23478):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at org.json.JSONTokener.nextValue(JSONTokener.java:94)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at org.json.JSONObject.<init>(JSONObject.java:155)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at org.json.JSONObject.<init>(JSONObject.java:172)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at com.myapp.MyActivity$MyAsyncTask.onPostExecute(MyActivity.java:377)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at com.myapp.MyActivity$MyAsyncTask.onPostExecute(MyActivity.java:1)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at android.os.AsyncTask.finish(AsyncTask.java:632)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at android.os.Looper.loop(Looper.java:136)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at android.app.ActivityThread.main(ActivityThread.java:5146)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at java.lang.reflect.Method.invokeNative(Native Method)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at java.lang.reflect.Method.invoke(Method.java:515)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
03-02 15:00:22.466: E/AndroidRuntime(23478):    at dalvik.system.NativeStart.main(Native Method)

如何处理这些事件?

这个概念被称为异常和异常处理。没有互联网接入是一种特殊情况,处理它的方法是将所有此类代码包含在其中:

try {
   // all your code which can throw an exception   
} catch (Exception e) { 
   // handler; e.g. show an error dialog
}

onPostExecute中创建JSON对象之前,您应该检查Strings是否为null。如果不是null,继续你正在做的任何事情,如果是null,请重新检查互联网连接并重试下载或任何你想要的东西。

相关内容

  • 没有找到相关文章

最新更新