簡體   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