简体   繁体   English

在Android onPostExecute中显示警报对话框或吐司

[英]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? 如果我得到json响应为OK,那么我需要继续进行下去,如果我遇到任何错误,那么我只想在Toast或Alert对话框中打印该错误,但无法正常工作。有人可以告诉我我的问题是什么第二,如果条件在代码? y it is not able to show toast? y它不能显示吐司吗?

This is the json result i get on error 这是我得到错误的json结果

{"result":"error","error":"invalid parameters"} {“结果”:“错误”,“错误”:“无效参数”}

i need to display that invalid parameters in Toast or Alert dialog. 我需要在Toast或Alert对话框中显示该无效参数。

    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 您的代码正在获取异常,因为在发生错误的情况下获得的响应json不包含以下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() . 问题是您应该使用getApplicationContext()而不是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. 如您的代码所示,您位于AsyncTask的onPostExecute()中,此时您的上下文(活动?)不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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