从JSON(Volley Request) Java创建对象



我遇到了一个问题,我想使用一个GET请求,然后从返回的JSON中创建一个对象。我让它在onclick侦听器中工作,但我不知道如何使构造函数接受响应。

这是我的媒体类它将从打开的电影数据库中获取url

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

}

另一个问题,这是一个很好的实现吗?还是有更快更好的方法?

最好的方法是使用Gson。Gson自动将JSON解析为POJO,反之亦然。

事实上,使用自定义请求,您实际上可以使volley返回POJO(Java对象)而不是JSON。

这篇文章给出了一个很好的一步一步的解释

您需要做的是创建一个空构造函数,并在Response回调中将您在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:但是,请记住这是异步的,在执行回调之前您不会设置属性。如果您想执行同步请求,请查看以下内容:

作为实现,我宁愿获得JSONObject响应并直接在对象中反序列化它。

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

最新更新