How to show toast according to the JSONObject response

旧巷老猫 提交于 2019-12-25 05:28:07

问题


I am doing an application in which I'm using username and password for log in, if log in is successful I'm getting user_id and all, but when invalid user try to log in, will have to show toast message to the user. I'm getting {"error":"invalid_grant"} as my JSONresponse and I'm able to get JSONObject in a string.

JSONObject jObject = new JSONObject(result);
String aJsonString = jObject.getString("error");

Here aJsonString have the value of error in it.

Now, my question is: How to show an error message when invalid user tries to log in? And, if the user is valid pass the user to postExecute() method for further action.


回答1:


JSONObject jObject;

    try {
        jObject = new JSONObject(sampledata);
        if (jObject.has("error")) {
String aJsonString = jObject.getString("error");
Toast.makeText(ActivityName.this, aJsonString, Toast.LENGTH_LONG).show();
        } else {
    Toast.makeText(ActivityName.this, "Login Successful", Toast.LENGTH_LONG).show();
        }
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


来源:https://stackoverflow.com/questions/15198309/how-to-show-toast-according-to-the-jsonobject-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!