Basit
Basit

Reputation: 8606

How to handle exception in AsyncTask

I am using AsyncTask like this

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

task:

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

My this task call another AsyncTask which has the same code. The problem is if server is running then everything fine. But if server is not running and i call the web service then i get the exception that Force Application close. Why? I am handling SocketTimeoutException IF exception happens then i am saying that show toast on my Activity but why it is force closing the application. How can i handle it? Thanks

Upvotes: 2

Views: 1647

Answers (2)

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

you should not use use Toast in doInBackground

 catch (SocketTimeoutException e) {                              

   Toast.makeText(accountReportActivity, "Service is not connected, Please make sure your server is running", Toast.LENGTH_LONG).show();  //<----------------------

Upvotes: 2

Nirali
Nirali

Reputation: 13805

You can check the internet connection this way first

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

In this method

       // 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();
        }

Upvotes: 2

Related Questions