繁体   English   中英

如何从Volley的onResponse返回值?

[英]How can I return value from onResponse of Volley?

我想在用户类变量上保存Response的值。 不幸的是,我不能。

请帮帮我。

这是代码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);

这是User类:

public class User {

    public int id;
    public String name;
    public String email;

}

这是API的JSON:

{"user":{"id":12,"name":"test_name","email":"test@email.com",}}

为您提供两点:

1.您解析emailname属性。

  userClass.name = jsonUser.getString("name");

2.您的json数据由于最后一个','

{"user":{"id":12,"name":"test_name","email":"test@email.com"}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM