Android中AsyncTask中的连接丢失处理

时间:2015-02-12 05:21:33

标签: android android-asynctask

在我的应用程序中,我已经使用REST API读取了DataBase SqlServer中的所有记录。但是在受保护的DetailsTimeTable doInBackground(String ... params)的中间执行时连接丢失时存在一些问题。然后我的应用程序关闭错误不幸的是应用程序停止 这是我的代码:

public class Monday_Time extends ListActivity {
    ArrayList<String> itemsList = new ArrayList<String>();
    private ProgressDialog progressDialog;
    private BroadcastReceiver mconn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time_table_list);
        // Bundle bundle = getIntent().getExtras();
        Toast.makeText(this, MainActivity.branch_static, Toast.LENGTH_LONG).show();
        // registerReceiver(new NetworkBroadCast(), new IntentFilter(ConnectivityManager.EXTRA_IS_FAILOVER));
        mconn = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
                boolean isfailOver = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
                NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

                Log.d("Connectivity", Boolean.toString(noConnectivity));
                Log.d("Reson ", reason);
                Log.d("FailOver", Boolean.toString(isfailOver));
            }
        };
        registerReceiver(mconn, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

        new AsyncDetailsTimeTable().execute(MainActivity.branch_static.trim(), MainActivity.sem_static.trim(), MainActivity.sec_static.trim(), "Monday");
    }

    public class AsyncDetailsTimeTable extends AsyncTask<String, Void, DetailsTimeTable> {
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progressDialog = new ProgressDialog(Monday_Time.this);
            progressDialog.setMessage("Loding Time Table....");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected void onPostExecute(DetailsTimeTable result) {
            // TODO Auto-generated method stub
            // Log.d("POST DATA", result.getFirst());
            progressDialog.dismiss();

            if (SSTCTimeTableTabActivity.ram == 1) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Monday_Time.this);
                builder.setMessage("Time Table is not availble....")
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // do things
                            startActivity(new Intent(Monday_Time.this, com.src.sstctimetable.MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                        }
                    });
                AlertDialog alert = builder.create();
                alert.show();
            } else {
                itemsList.add("1:: " + result.getFirst());
                itemsList.add("2:: " + result.getSecond());
                itemsList.add("3:: " + result.getThird());
                itemsList.add("4:: " + result.getFourth());
                itemsList.add("5:: " + result.getFifth());
                itemsList.add("6:: " + result.getSixth());
                itemsList.add("7:: " + result.getSeventh());

                setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.rowlayout, R.id.label, itemsList));
            }
            super.onPostExecute(result);
        }

        @Override
        protected DetailsTimeTable doInBackground(String... params) {
            DetailsTimeTable userDetail = null;
            RestAPI api = new RestAPI();
            try {
                JSONObject jsonObj = api.GetTimeTableDetails(params[0], params[1], params[2], params[3]);
                JSONParser parser = new JSONParser();
                userDetail = parser.parseUserDetails(jsonObj);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("AsyncUserDetails", e.getMessage());
            }
            return userDetail;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

catch (Exception e) {
    // TODO Auto-generated catch block 
    // Log.d("AsyncUserDetails", e.getMessage());
}

尝试发表评论Log。我认为e.getmessage()投掷NullPointerException