我需要使用缓存来提供脱机支持,试图将NetworkResponse集成到我的Volley中,但是它不会更新。
public void loadRecyclerViewData() {
JSONObject postparams = new JSONObject();
String country = getIntent().getStringExtra("country");
String windProbability = getIntent().getStringExtra("windProbability");
if(country==null){
country="";
windProbability="";
}
try {
postparams.put("country", country);
if (windProbability.equals("")) {
postparams.put("windProbability", windProbability);
}
else {
postparams.put("windProbability", Integer.parseInt(windProbability));
}
} catch (JSONException e) {
e.printStackTrace();
}
// Log.d(TAG,postparams.toString());
JsonObjectRequest listRequest = new JsonObjectRequest(Request.Method.POST, URL_DATA + "api-spot-get-all", postparams,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray array = response.getJSONArray("result");
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
int longitude = 0;
int latitude = 0;
int windProbability = 0;
ListItem list = new ListItem(
o.getString("id"),
o.getString("name"),
longitude,
latitude,
windProbability,
o.getString("country"),
o.getString("whenToGo"),
o.getBoolean("isFavorite")
);
listItems.add(list);
}
ListItemAdapter listItemAdapter = new ListItemAdapter(getApplicationContext(), listItems);
recyclerView.setAdapter(listItemAdapter);
listItemAdapter.token = token;
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<>();
headers.put("token", token);
return headers;
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
final long cacheHitButRefreshed = 1000; // in 3 minutes cache will be hit, but also refreshed on background
final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
long now = System.currentTimeMillis();
final long softExpire = now + cacheHitButRefreshed;
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = softExpire;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null) {
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString), cacheEntry);
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(JSONObject response) {
super.deliverResponse(response);
}
@Override
public void deliverError(VolleyError error) {
super.deliverError(error);
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
};
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(listRequest);
}
示例:当我返回时,我将添加到项目收藏夹更改活动中,将其转到最初的收藏夹项目。
它应该更新数据
我在每个活动中的每个排球上都添加了parseNetworkResponse,这里的代码可能只是最重要的一部分