2017-03-08 6 views
0

응답에서 값을 사용자 클래스 변수에 저장하려고합니다. 불행히도, 나는 할 수 없습니다.발리의 onResponse에서 어떻게 값을 반환 할 수 있습니까?

도와주세요. 당신을 위해

{"user":{"id":12,"name":"test_name","email":"[email protected]",}} 
+0

에 잘못된 것입니다. –

+0

Setter Getter를 사용하고 사용자 클래스 개체 "userClass"를 전달하십시오. 여기에서 값을 사용하고 싶습니다. – Chandan

답변

0

두 지점 :

public class User { 

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

} 

이이 API의 JSON입니다 :

이이 사용자 클래스입니다

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); 

JSONObjectRequest 코드입니다

1You 구문 분석 email ~ name 속성

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

2.your의 JSON 데이터는 it.It 쓰레기 수집 될 만들기하지 않는 경우 때문에 사용자 클래스에서 세터 게터를 적용하십시오 인해 지난 ','

{"user":{"id":12,"name":"test_name","email":"[email protected]"}}