[英]How can I return value from onResponse of Volley?
I want to save values from Response on user class variables. 我想在用户类变量上保存Response的值。 Unfortunately, I cannot.
不幸的是,我不能。
Help me please. 请帮帮我。
This is the code JSONObjectRequest 这是代码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: 这是User类:
public class User {
public int id;
public String name;
public String email;
}
This is the JSON of the API: 这是API的JSON:
{"user":{"id":12,"name":"test_name","email":"test@email.com",}}
Two points for you : 为您提供两点:
1.you parse email
to name
attribude. 1.您解析
email
以name
属性。
userClass.name = jsonUser.getString("name");
2.your json data is wrong due to the last ','
2.您的json数据由于最后一个
','
{"user":{"id":12,"name":"test_name","email":"test@email.com"}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.