0

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?

Pr0tonion
  • 187
  • 2
  • 12

4 Answers4

0

What you need to do is create an empty constructor, and in the Response callback set what you received in the JSON to the attributes of your class.

Something like this:

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: But, have in mind that this is asynchronous, you won't have the attributes set until the callback is executed. If you want to do a synchronous request, take a look at this.

Community
  • 1
  • 1
Dani M
  • 1,173
  • 1
  • 15
  • 43
0

As an implementation I would rather get the JSONObject response and deserialize it in an object directly.

lukaspp
  • 1,059
  • 10
  • 15
0
Create ModelClass for the fields and use Gson to make object of class from JSON response.
Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38
0

The best way would be using Gson. Gson automatically parses JSON to POJO and vice versa.

Infact with a custom request you can actually make volley return a POJO(Java object) instead of JSON.

This article gives a very good step by step explanation for it

Irshad Kumail
  • 1,243
  • 11
  • 10