I am having a problem, I want to use a GET request with volley, then create an object from the JSON that is returned. I get it to work inside of a onclick listener but I got no clue on how to make the constructor take the response.
This is my media class which will get a url from open movie database
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;
}
}
Another question, is this a good implementation? Or is there a way which is faster and better?