我如何缓存jsonobjectrequest?

时间:2016-06-08 02:44:19

标签: java android caching jsonobjectrequest

我的RecyclerView适配器声明为

 adapterlist = new AdapterList(getActivity(),getData());

并且getData()函数定义为

public List<Information> getData(){
    final List<Information> data = new ArrayList<>();
    showpDialog();
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.GET, urlfood, null, new Response.Listener<JSONObject>() {


                @Override
                public void onResponse(JSONObject jsonObject) {
                    for (int i = 0; i < 10; i++) {
                    try {


                        JSONArray items = jsonObject.getJSONObject("response").getJSONArray("groups").getJSONObject(0).getJSONArray("items");



                           if (jsonObject.getJSONObject("response").getJSONArray("groups").getJSONObject(0).getJSONArray("items").getJSONObject(i).getJSONObject("venue").has("location")) {
                               if(jsonObject.getJSONObject("response").getJSONArray("groups").getJSONObject(0).getJSONArray("items").getJSONObject(i).getJSONObject("venue").getJSONObject("location").has("address")){

                                JSONObject location = jsonObject.getJSONObject("response").getJSONArray("groups").getJSONObject(0).getJSONArray("items").getJSONObject(i).getJSONObject("venue").getJSONObject("location");
                                Address[i] = location.getString("address");}}

                            Name[i] = items.getJSONObject(i).getJSONObject("venue").getString("name");
                            String rtg=items.getJSONObject(i).getJSONObject("venue").getString("rating");
                            Rating[i]=Float.parseFloat(rtg);
                            Information current = new Information();
                            current.name = Name[i];

                            current.address = Address[i];
                            current.ratings = Rating[i];
                            data.add(current);


                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
                    recyclerView.setAdapter(adapterlist);
                    hidepDialog();

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getActivity(),
                            "error:"+error.getMessage(), Toast.LENGTH_SHORT).show();

                   hidepDialog();
                }
            }


            );
    AppController.getInstance().addToRequestQueue(jsonObjectRequest);


    return data;
}

这些东西在扩展片段的类中定义。 如何缓存原始响应或解析的响应?请帮忙。

我已经在StackOverflow上检查了很多答案,但我无法理解如何将它们集成到我的片段类中。

0 个答案:

没有答案