I am registering a user through laravel api. Response is coming in json object. Now I want to print message string of this object in Toast instead of writing "Acount Successfully created". what should I do.
JSON Object
{ "success": true, "data": { "token": "//removed", "name": "Abdullah" }, "message": "User register successfully." }
Here is the onResponse Method
OnResponse
@Override
public void onResponse(JSONObject response) {
try {
if ( response.getBoolean("success")) {
Log.i("response", response.toString());
Toast.makeText(RegisterActivity.this, "Account Successfully Created", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
} else {
Log.e("Response", response.toString());
Toast.makeText(RegisterActivity.this, "" + response, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
You can access to the properties using the get(String) method and then use the asText(); to get the value of the property as String:
public void onResponse(JSONObject response) {
try {
if (response.getBoolean("success")) {
Log.i("response", response.toString());
Toast.makeText(RegisterActivity.this, response.get("message").toString(), Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
} else {
Log.e("Response", response.toString());
Toast.makeText(RegisterActivity.this, "" + response, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}