在没有Gson的情况下使用Volley [英] Using Volley without Gson

查看:162
本文介绍了在没有Gson的情况下使用Volley的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我知道Retrofit使用gson(或任何其他转换器)对json响应(使用okhttp或任何相关库获得的响应)进行序列化或反序列化. 现在,当我还是天真的时候(从某种意义上说仍然是),我曾经使用Volley,那时我从来没有使用过Gson或任何相关的库,也没有使用过okhttp. .

Today I got to know that Retrofit uses gson(or any other convertor) to serialize or deserialize the json response (response that was got using okhttp or any related library). Now, when I was naive(in some sense still am) and I used to use Volley and at that time I never used Gson or any related library and same for okhttp.But i used to get my response and inflate it successfully on my views.

1.现在,Volley是否在内部使用Gson和Okhttp做Retrofit的工作? 如果不? 2.那我怎么能不使用任何东西来解析值呢?

以下是我曾经编写的示例代码:-

Below is the sample Codes that i used to write:-

  JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(
            Request.Method.POST, URL_THUMB, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray=response.getJSONArray("server_response");
                for(int i=0;i<jsonArray.length();i++)
                {
                    JSONObject jsonObject=(JSONObject)jsonArray.get(i);
                    String id=jsonObject.getString("id");
                    String artist_name=jsonObject.getString("artist_name");
                    String img_id=jsonObject.getString("img_id");

                    listId.add(id);
                    listArtistName.add(artist_name);
                    listImgID.add(img_id);

                }

                recyclerView.setAdapter(comedy_adapter);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }
    );

现在将这些列表值添加到我的视图中.

and now just inflate these list values to my views.

我哪里出错了? (我不认为我做错了,因为一切进展顺利,代码始终可以正常运行)

Where did I go wrong? (I don't think I was wrong as things went well and code always run fine)

推荐答案

在您的示例中,您将响应手动解析为JSON数组和对象.诸如Gson之类的转换器使您可以将响应解析为一行中的自定义对象的变量.

In your example you're parsing the response into JSON arrays and objects manually. Converters such as Gson let you parse the response into a variable of a custom object in a single line.

例如,如果我有以下模型:

For example, if I have the following model:

public class Model {
    private int id;
    private String name; 
}

我可以使用以下代码来解析字符串响应:

I can parse a string response using the following code:

Model model = gson.fromJson(str, Model.class);

否则,您必须手动执行此操作,例如当前操作:

Otherwise, you have to do it manually, like what you're doing at the moment:

JSONObject jsonObject = response.getJSONObject("str");
int id = jsonObject.getInt("id");
String name = jsonObject.getString("name");
Model model = new Model(id, name);

在Retrofit 2中,您甚至不必调用fromJson-您只需在onResponse中简单地将所需的对象作为输入参数即可.在处理更复杂的模型时非常有用.

In Retrofit 2 you don't even have to call fromJson - you simple receive the object you expect as an input parameter in onResponse. It's very useful when dealing with more complex models.

这篇关于在没有Gson的情况下使用Volley的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆