简体   繁体   中英

Displaying Alert dialog or toast in Android onPostExecute

if i get json response as OK then i need to proceed further, if i get any error then i want to print only that error in the Toast or Alert dialog, but its not working.can anybody please tell me what is the problem with my second if condition in the code? y it is not able to show toast?

This is the json result i get on error

{"result":"error","error":"invalid parameters"}

i need to display that invalid parameters in Toast or Alert dialog.

    protected void onPostExecute(String result) {
        prgDialog.hide();
        Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
        try {
            JSONObject json = new JSONObject(result);
            String respResult=json.getString("result");
            String respId=json.getString("customer_id");
            String respEmail=json.getString("customer_email");
            String respError=json.getString("error");
            session.createUserLoginSession(respId, respEmail);
            if(respResult.equals("ok")) {
                Intent I = new Intent();
                I.setClass(getApplicationContext(), MainActivity.class);
                startActivity(I);
                finish();
            }
            if(respResult.equals("error")) {
                Toast.makeText(getBaseContext(), respError, Toast.LENGTH_LONG).show();
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

Your code is getting an exception because the response json you get in case of error, does not contain below params

"customer_id"
"customer_email"

Write your code like this

 try {
            JSONObject json = new JSONObject(result);
            String respResult=json.getString("result");

            if(respResult.equals("ok")) {

            String respId=json.getString("customer_id");
            String respEmail=json.getString("customer_email");
            session.createUserLoginSession(respId, respEmail);

                Intent I = new Intent();
                I.setClass(getApplicationContext(), MainActivity.class);
                startActivity(I);
                finish();
            }
            if(respResult.equals("error")) {
                String respError=json.getString("error");
                Toast.makeText(getApplicationContext(), respError, Toast.LENGTH_LONG).show();
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Why you put error variable in above? it should be

protected void onPostExecute(String result) {
        prgDialog.hide();
        Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
        try {
            JSONObject json = new JSONObject(result);
            String respResult=json.getString("result");

            if(respResult.equals("ok")) {
                String respId=json.getString("customer_id");
                String respEmail=json.getString("customer_email");
                session.createUserLoginSession(respId, respEmail);
                Intent I = new Intent();
                I.setClass(getApplicationContext(), MainActivity.class);
                startActivity(I);
                finish();
            }
            if(respResult.equals("error")) {
                String respError=json.getString("error");
                Toast.makeText(getApplicationContext(), respError, Toast.LENGTH_LONG).show();
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

The problem is that you should use getApplicationContext() instead of getBaseContext() . The first is always available when your app is running and the second one, can be not available anymore. As seen in your code you are in onPostExecute() in AsyncTask and your context (activity?) could not be available at that time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM