2

我正在使用这样的 AsyncTask

public class AccountReportActivity extends Activity {

private OrgaLevelTask orgaLevelTask;

    public void onCreate(Bundle savedInstanceState) {            
        ......
        orgaLevelTask  = new OrgaLevelTask(AccountReportActivity.this, spinner_orgaLevel, spinner_branch, txt_extra, txt_extra1);               
        orgaLevelTask.execute();

    } //end of onCreate

} //end of class AccountReportActivity

任务:

public class OrgaLevelTask extends AsyncTask<Void, Void, ArrayList<OrgaLevel>> {

    //Constrcutor
    public OrgaLevelTask(AccountReportActivity accountReportActivity, Spinner spinner_orgaLevel, Spinner spinner_branch, TextView txt_extra, TextView txt_extra1) {

        this.accountReportActivity = accountReportActivity; 
        this.spinner_orgaLevel = spinner_orgaLevel;
        ....                
    } //end of constructor

    @Override
    protected ArrayList<OrgaLevel> doInBackground(Void... arg0) {       
        return callWebService();        
    } //end of doInBackground()

    private ArrayList<OrgaLevel> callWebService() {

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            ......
        } catch (SocketTimeoutException e) {                 
             Toast.makeText(accountReportActivity, "Service is not connected, Please make sure your server is running", Toast.LENGTH_LONG).show();
             return null;                                   
        }  catch(Exception e) {             
            e.printStackTrace();
            System.out.println();                               
        }
    } //end of callWebService()

} //end of class OrgaLevelTask

我的这个任务调用另一个具有相同代码的 AsyncTask。问题是如果服务器正在运行,那么一切都很好。但是,如果服务器没有运行并且我调用了 Web 服务,那么我会得到Force Application close的异常。为什么?我正在处理SocketTimeoutException如果发生异常然后我说在我的 Activity 上显示 toast 但为什么它会强制关闭应用程序。我该如何处理?谢谢

4

2 回答 2

2

您可以先通过这种方式检查互联网连接

public static boolean checkConnection(Context context) {

    final ConnectivityManager mConnectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else
        return false;
 }

在这个方法中

       // Checking the internet connection
        if (!InternetConnectionCheck.checkConnection(this)) {
            Utilities.alertDialogBox(this, R.string.Title_String,
                    R.string.No_Internet_connection_String);
        } else {
           orgaLevelTask  = new OrgaLevelTask(AccountReportActivity.this, spinner_orgaLevel, spinner_branch, txt_extra, txt_extra1);               
           orgaLevelTask.execute();
        }
于 2012-07-18T05:11:39.227 回答
2

你不应该使用ToastdoInBackground

 catch (SocketTimeoutException e) {                              

   Toast.makeText(accountReportActivity, "Service is not connected, Please make sure your server is running", Toast.LENGTH_LONG).show();  //<----------------------
于 2012-07-18T05:24:04.997 回答