如何处理BIG AyncTask上的互联网连接变化

时间:2023-01-05 16:56:03

I'm developing an Android application that do a big work in a AsyncTask (can be 5 minutes of AsyncTask activity).

我正在开发一个在AsyncTask中做大工作的Android应用程序(可以是5分钟的AsyncTask活动)。

This Task need the internet connection so at the start I check if the device is online or not.

此任务需要互联网连接,所以在开始时我检查设备是否在线。

BUT IF the user change connection from wi-fi to mobile connection or go offline when the GraphRequest is running i got a crash with this error:

但是,如果用户将连接从Wi-Fi更改为移动连接,或者在GraphRequest运行时脱机,则会出现此错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONObject.getJSONObject(java.lang.String)' on a null object reference

引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'org.json.JSONObject org.json.JSONObject.getJSONObject(java.lang.String)'

Here is the part of the code where crash:

以下是崩溃代码的一部分:

// I Check if the device is online, if yes:

for(j=0; j<n_foto_pub;j++) {

        GraphRequest request2 = GraphRequest.newGraphPathRequest(
                AccessToken.getCurrentAccessToken(),
                "MY_QUERY",
                new GraphRequest.Callback() {
                    @Override
                    public void onCompleted(GraphResponse response) {

                        try {                                
                            JSONObject obj1 = response.getJSONObject();
                            JSONObject obj2 = obj1.getJSONObject("data1");  <--- CRASH HERE
                            JSONObject obj3 = obj2.getJSONObject("data2");  
                            int ris = obj3.getInt("ris");                            
                            ..........

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
        request2.executeAndWait();
        }

I have a catch but crash anyways.

我有一个捕获但反正崩溃。

How can i avoid the app crash and instead maybe kick the user back on the previous activity?

我怎样才能避免应用程序崩溃,而是让用户重新启动之前的活动?

1 个解决方案

#1


1  

See below code , u will get better idea.

看下面的代码,你会得到更好的主意。

    @Override
  protected String doInBackground(String... params) {
  {
    try
    {
        //Your operation always inside try catch block to avoid crash.
        for(j=0; j<n_foto_pub;j++) 
        {
           if(checkInternet()) //check internet for each loop
           {

           }
           else
           {
             callBackActivity();
           }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
         callBackActivity();
    }
  }

#1


1  

See below code , u will get better idea.

看下面的代码,你会得到更好的主意。

    @Override
  protected String doInBackground(String... params) {
  {
    try
    {
        //Your operation always inside try catch block to avoid crash.
        for(j=0; j<n_foto_pub;j++) 
        {
           if(checkInternet()) //check internet for each loop
           {

           }
           else
           {
             callBackActivity();
           }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
         callBackActivity();
    }
  }