2016-10-14 6 views
0

問題があります。ボレーでGETリクエストを使用し、JSONからオブジェクトを作成して返します。私はそれをonclickリスナーの内部で動作させることにしますが、コンストラクタにレスポンスを取る方法についての手がかりはありません。JSON(Volley Request)からオブジェクトを作成する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に、またはその逆に解析します。

実際にカスタムリクエストを行うと、実際にvolleyに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); 
} 

EDIT:コールバックが実行されるまでしかし、これが非同期であることを心に持っている、あなたは属性が設定されていません。同期要求を行う場合は、thisをご覧ください。

0

私はむしろJSONObjectレスポンスを取得し、それをオブジェクト内で直接デシリアライズします。

0
Create ModelClass for the fields and use Gson to make object of class from JSON response. 
関連する問題