问题
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