我想将响应中的值保存在用户类变量上。不幸的是,我不能。
请帮帮我。
这是代码 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);
这是用户类:
public class User {
public int id;
public String name;
public String email;
}
这是 API 的 JSON:
{"user":{"id":12,"name":"test_name","email":"test@email.com",}}