0

我想将响应中的值保存在用户类变量上。不幸的是,我不能。

请帮帮我。

这是代码 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",}}
4

1 回答 1

0

给你两点:

email1.你解析name属性。

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

2.你的json数据是错误的,因为最后','

{"user":{"id":12,"name":"test_name","email":"test@email.com"}}
于 2017-03-08T03:13:21.817 回答