2016-10-14 2 views
0

문제가 발생했습니다. 발리와 함께 GET 요청을 사용하고 JSON에서 객체를 생성합니다. 나는 그것을 onclick 청취자 내부에서 작업하도록 만들었지 만, 생성자가 응답을받는 방법에 대한 단서가 없습니다.JSON (발리 요청)에서 객체 생성 Java

이, 이것은 좋은 구현 열린 영화 데이터베이스에서

public class Media extends MainActivity{ 
private String title; 
private String yearReleased; 
private String rated; 
private String director; 
private String actors; 
private String plot; 
private String posterUrl; 
private String type; 
final String TAG = AppController.class.getSimpleName(); 


public Media(String title, String yearReleased, String rated, String director, String actors, String plot, String posterUrl, String type) { 


    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.d(TAG, response.toString()); 
        try { 
         title = 
         rated = response.getString("Year"); 
         //String rated = response.getString("Rated"); 
         //String released = response.getString("Released"); 
         //String genre = response.getString("Genre"); 
         //String director = response.getString("Director"); 
         //String actors = response.getString("Actors"); 
         //String plot = response.getString("Plot"); 
         //String language = response.getString("Language"); 
         //String awards = response.getString("Awards"); 
         //String poster = response.getString("Title"); 
         //String imdbRating = response.getString("imdbRating"); 
         //String type = response.getString("Type"); 




        } catch (JSONException e) { 
         e.printStackTrace(); 
         Toast.makeText(getApplicationContext(), 
           "Error: " + e.getMessage(), 
           Toast.LENGTH_LONG).show(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        Toast.makeText(getApplicationContext(), 
          error.getMessage(), Toast.LENGTH_SHORT).show(); 
       } 
      } 

    ); 
    AppController.getInstance().addToRequestQueue(jsonObjectRequest); 



    this.title = title; 
    this.yearReleased = yearReleased; 
    this.rated = rated; 
    this.director = director; 
    this.actors = actors; 
    this.plot = plot; 
    this.posterUrl = posterUrl; 
    this.type = type; 
} 

public Media(View.OnClickListener mainActivity) { 
    super(); 
} 

public void getJsonObject() { 

} 

//@Override 
//public String getTitle() {return title;} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getYearReleased() { 
    return yearReleased; 
} 

public void setYearReleased(String yearReleased) { 
    this.yearReleased = yearReleased; 
} 

public String getRated() { 
    return rated; 
} 

public void setRated(String rated) { 
    this.rated = rated; 
} 

public String getDirector() { 
    return director; 
} 

public void setDirector(String director) { 
    this.director = director; 
} 

public String getActors() { 
    return actors; 
} 

public void setActors(String actors) { 
    this.actors = actors; 
} 

public String getPlot() { 
    return plot; 
} 

public void setPlot(String plot) { 
    this.plot = plot; 
} 

public String getPosterUrl() { 
    return posterUrl; 
} 

public void setPosterUrl(String posterUrl) { 
    this.posterUrl = posterUrl; 
} 

public String getType() { 
    return type; 
} 

public void setType(String type) { 
    this.type = type; 
} 

}

또 다른 질문을 URL을 얻을 것이다 내 미디어 클래스이다? 아니면 더 빠르고 더 좋은 방법이 있습니까?

답변

0

가장 좋은 방법은 Gson을 사용하는 것입니다. Gson은 자동으로 JSON을 POJO로 구문 분석하고 그 반대도 마찬가지입니다.

실제로 사용자 정의 요청을 사용하면 실제로 발리가 JSON 대신 POJO (Java 객체)를 반환 할 수 있습니다.

This article은 단계 설명으로 매우 좋은 단계를 제공합니다.

0

빈 생성자를 만들고 응답 콜백 세트에서 JSON에서받은 것을 클래스의 속성으로 설정해야합니다.

이런 식으로 뭔가 :

public Media() 
{ 
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.d(TAG, response.toString()); 
        try { 
         this.year = response.getString("Year"); 
         this.rated = response.getString("Rated"); 


         ... 


        } catch (JSONException e) { 
         e.printStackTrace(); 
         Toast.makeText(getApplicationContext(), 
           "Error: " + e.getMessage(), 
           Toast.LENGTH_LONG).show(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        Toast.makeText(getApplicationContext(), 
          error.getMessage(), Toast.LENGTH_SHORT).show(); 
       } 
      } 

    ); 

    AppController.getInstance().addToRequestQueue(jsonObjectRequest); 
} 

편집 :하지만,이 비동기, 당신은 콜백이 실행 될 때까지 속성이 설정되어 있지 않습니다 염두에두고있다. 동기식 요청을하려면 this을 살펴보십시오.

0

구현으로서 나는 JSONObject 응답을 얻고 객체에서 직접 deserialize하려고합니다.

0
Create ModelClass for the fields and use Gson to make object of class from JSON response.