我使用volley框架从服务器检索json响应,但每次从服务器加载json时。我如何缓存此json响应以便在脱机中使用它并与新响应同步。以下是我从服务器检索json响应的代码
// Creating volley request obj
JsonArrayRequest Req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
MainPojo test = new MainPojo();
String name = obj.getString("facility_name");
test.setName(name);
//test.setUrl(obj.getString("easyurl"));
testList.add(test);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
})
{
//**
// Passing some request headers
//*
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Cookie", MainActivity.sharedpreferences.getString(savedCookie, ""));
headers.put("Set-Cookie", MainActivity.sharedpreferences.getString(savedCookie, ""));
headers.put("Content-Type", "application/x-www-form-urlencoded");
//headers.put("Content-Type","application/json");
headers.put("Accept", "application/x-www-form-urlencoded");
return headers;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(Req);
}
请让我知道如何缓存json响应
答案 0 :(得分:0)
也许我错过了什么,但尝试使用共享偏好