如何从 Volley 的 onResponse 中获得 return 值?

How can I return value from onResponse of Volley?

我想保存用户 class 变量的 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);

这是用户 class:

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.you 将 email 解析为 name 属性。

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

2.your json 由于最后 ','

数据错误
{"user":{"id":12,"name":"test_name","email":"test@email.com"}}