I want to save values from Response on user class variables. Unfortunately, I cannot.
Help me please.
This is the code JSONObjectRequest
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
jsonURLFull, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(ActivityLogin.class.getSimpleName(), response.toString());
try {
User userClass = new User();
JSONObject jsonUser = response.getJSONObject("user");
userClass.id = jsonUser.getInt("id");
userClass.name = jsonUser.getString("email");
userClass.email = jsonUser.getString("email");
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(ActivityLogin.class.getSimpleName(), "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
This is the User class:
public class User {
public int id;
public String name;
public String email;
}
This is the JSON of the API:
{"user":{"id":12,"name":"test_name","email":"[email protected]",}}
Two points for you :
1.you parse email
to name
attribude.
userClass.name = jsonUser.getString("name");
2.your json data is wrong due to the last ','
{"user":{"id":12,"name":"test_name","email":"[email protected]"}}