猿问

Android设置Volley以从缓存中使用

我正在尝试为服务器JSON响应创建并使用缓存。


例如:


将JSON对象缓存到内部存储器中,并在没有互联网连接时使用它。


在下面的示例代码中,我找不到任何有关如何对其进行缓存Volley并在服务器头再次更新未到期时重新使用的文档。


像这样:将过期设置为标头并使用缓存,并在过期后尝试再次加载。


我正在尝试为此方法设置缓存机制:


private void makeJsonArryReq() {

    JsonArrayRequest req = new JsonArrayRequest(Const.URL_JSON_ARRAY,

            new Response.Listener<JSONArray>() {

                @Override

                public void onResponse(JSONArray response) {

                    msgResponse.setText(response.toString());

                }

            }, new Response.ErrorListener() {

        @Override

        public void onErrorResponse(VolleyError error) {

        }

    });

    AppController.getInstance().addToRequestQueue(req,tag_json_arry);

}

缓存方法:


public static Cache.Entry parseIgnoreCacheHeaders(NetworkResponse response) {

    long now = System.currentTimeMillis();


    Map<String, String> headers = response.headers;

    long serverDate = 0;

    String serverEtag = null;

    String headerValue;


    headerValue = headers.get("Date");

    if (headerValue != null) {

        serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);

    }


    serverEtag = headers.get("ETag");


    final long cacheHitButRefreshed = 3 * 60 * 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

    final long softExpire = now + cacheHitButRefreshed;

    final long ttl = now + cacheExpired;


    Cache.Entry entry = new Cache.Entry();

    entry.data = response.data;

    entry.etag = serverEtag;

    entry.softTtl = softExpire;

    entry.ttl = ttl;

    entry.serverDate = serverDate;

    entry.responseHeaders = headers;


    return entry;

}


人到中年有点甜
浏览 616回答 2
2回答

慕慕森

请注意,如果Web服务支持缓存输出,则无需在CacheRequest下面使用,因为Volley它将自动缓存。对于您的问题,我在内部使用了一些代码parseCacheHeaders(并引用了@ oleksandr_yefremov的代码)。我测试了以下代码。当然也可以使用JsonArrayRequest。希望对您有所帮助!&nbsp; &nbsp; JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(0, mUrl, new Response.Listener<JSONObject>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(JSONObject response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTextView.setText(response.toString(5));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTextView.setText(e.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }, new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }) {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (cacheEntry == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry = new Cache.Entry();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long now = System.currentTimeMillis();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final long softExpire = now + cacheHitButRefreshed;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final long ttl = now + cacheExpired;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.data = response.data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.softTtl = softExpire;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.ttl = ttl;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String headerValue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headerValue = response.headers.get("Date");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (headerValue != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headerValue = response.headers.get("Last-Modified");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (headerValue != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.responseHeaders = response.headers;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String jsonString = new String(response.data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpHeaderParser.parseCharset(response.headers));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Response.success(new JSONObject(jsonString), cacheEntry);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (UnsupportedEncodingException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Response.error(new ParseError(e));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Response.error(new ParseError(e));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void deliverResponse(JSONObject response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.deliverResponse(response);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void deliverError(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.deliverError(error);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected VolleyError parseNetworkError(VolleyError volleyError) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return super.parseNetworkError(volleyError);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);更新:如果需要基类,请参考以下代码:public class CacheRequest extends Request<NetworkResponse> {&nbsp; &nbsp; private final Response.Listener<NetworkResponse> mListener;&nbsp; &nbsp; private final Response.ErrorListener mErrorListener;&nbsp; &nbsp; public CacheRequest(int method, String url, Response.Listener<NetworkResponse> listener, Response.ErrorListener errorListener) {&nbsp; &nbsp; &nbsp; &nbsp; super(method, url, errorListener);&nbsp; &nbsp; &nbsp; &nbsp; this.mListener = listener;&nbsp; &nbsp; &nbsp; &nbsp; this.mErrorListener = errorListener;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected Response<NetworkResponse> parseNetworkResponse(NetworkResponse response) {&nbsp; &nbsp; &nbsp; &nbsp; Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);&nbsp; &nbsp; &nbsp; &nbsp; if (cacheEntry == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry = new Cache.Entry();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background&nbsp; &nbsp; &nbsp; &nbsp; final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely&nbsp; &nbsp; &nbsp; &nbsp; long now = System.currentTimeMillis();&nbsp; &nbsp; &nbsp; &nbsp; final long softExpire = now + cacheHitButRefreshed;&nbsp; &nbsp; &nbsp; &nbsp; final long ttl = now + cacheExpired;&nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.data = response.data;&nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.softTtl = softExpire;&nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.ttl = ttl;&nbsp; &nbsp; &nbsp; &nbsp; String headerValue;&nbsp; &nbsp; &nbsp; &nbsp; headerValue = response.headers.get("Date");&nbsp; &nbsp; &nbsp; &nbsp; if (headerValue != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; headerValue = response.headers.get("Last-Modified");&nbsp; &nbsp; &nbsp; &nbsp; if (headerValue != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.responseHeaders = response.headers;&nbsp; &nbsp; &nbsp; &nbsp; return Response.success(response, cacheEntry);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void deliverResponse(NetworkResponse response) {&nbsp; &nbsp; &nbsp; &nbsp; mListener.onResponse(response);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected VolleyError parseNetworkError(VolleyError volleyError) {&nbsp; &nbsp; &nbsp; &nbsp; return super.parseNetworkError(volleyError);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void deliverError(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; mErrorListener.onErrorResponse(error);&nbsp; &nbsp; }}然后在MainActivity中,您可以像这样调用CacheRequest cacheRequest = new CacheRequest(0, mUrl, new Response.Listener<NetworkResponse>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(NetworkResponse response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String jsonString = new String(response.data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpHeaderParser.parseCharset(response.headers));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = new JSONObject(jsonString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTextView.setText(jsonObject.toString(5));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (UnsupportedEncodingException | JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }, new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTextView.setText(error.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; MySingleton.getInstance(this).addToRequestQueue(cacheRequest);用全源代码更新:MainActivity.java:package com.example.cachevolley;import android.content.Context;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;import android.widget.Toast;import com.android.volley.Cache;import com.android.volley.NetworkResponse;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.HttpHeaderParser;import com.android.volley.toolbox.Volley;import org.json.JSONException;import org.json.JSONObject;import java.io.UnsupportedEncodingException;public class MainActivity extends AppCompatActivity {&nbsp; &nbsp; private final Context mContext = this;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; final TextView textView = (TextView) findViewById(R.id.textView);&nbsp; &nbsp; &nbsp; &nbsp; RequestQueue queue = Volley.newRequestQueue(this);&nbsp; &nbsp; &nbsp; &nbsp; String url = "http://192.168.0.100/apitest";&nbsp; &nbsp; &nbsp; &nbsp; CacheRequest cacheRequest = new CacheRequest(0, url, new Response.Listener<NetworkResponse>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(NetworkResponse response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String jsonString = new String(response.data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpHeaderParser.parseCharset(response.headers));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = new JSONObject(jsonString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textView.setText(jsonObject.toString(5));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(mContext, "onResponse:\n\n" + jsonObject.toString(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (UnsupportedEncodingException | JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }, new Response.ErrorListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onErrorResponse(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(mContext, "onErrorResponse:\n\n" + error.toString(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; // Add the request to the RequestQueue.&nbsp; &nbsp; &nbsp; &nbsp; queue.add(cacheRequest);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onCreateOptionsMenu(Menu menu) {&nbsp; &nbsp; &nbsp; &nbsp; // Inflate the menu; this adds items to the action bar if it is present.&nbsp; &nbsp; &nbsp; &nbsp; getMenuInflater().inflate(R.menu.menu_main, menu);&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onOptionsItemSelected(MenuItem item) {&nbsp; &nbsp; &nbsp; &nbsp; // Handle action bar item clicks here. The action bar will&nbsp; &nbsp; &nbsp; &nbsp; // automatically handle clicks on the Home/Up button, so long&nbsp; &nbsp; &nbsp; &nbsp; // as you specify a parent activity in AndroidManifest.xml.&nbsp; &nbsp; &nbsp; &nbsp; int id = item.getItemId();&nbsp; &nbsp; &nbsp; &nbsp; //noinspection SimplifiableIfStatement&nbsp; &nbsp; &nbsp; &nbsp; if (id == R.id.action_settings) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return super.onOptionsItemSelected(item);&nbsp; &nbsp; }&nbsp; &nbsp; private class CacheRequest extends Request<NetworkResponse> {&nbsp; &nbsp; &nbsp; &nbsp; private final Response.Listener<NetworkResponse> mListener;&nbsp; &nbsp; &nbsp; &nbsp; private final Response.ErrorListener mErrorListener;&nbsp; &nbsp; &nbsp; &nbsp; public CacheRequest(int method, String url, Response.Listener<NetworkResponse> listener, Response.ErrorListener errorListener) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(method, url, errorListener);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.mListener = listener;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.mErrorListener = errorListener;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected Response<NetworkResponse> parseNetworkResponse(NetworkResponse response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (cacheEntry == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry = new Cache.Entry();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long now = System.currentTimeMillis();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final long softExpire = now + cacheHitButRefreshed;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final long ttl = now + cacheExpired;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.data = response.data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.softTtl = softExpire;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.ttl = ttl;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String headerValue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headerValue = response.headers.get("Date");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (headerValue != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headerValue = response.headers.get("Last-Modified");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (headerValue != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cacheEntry.responseHeaders = response.headers;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Response.success(response, cacheEntry);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void deliverResponse(NetworkResponse response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mListener.onResponse(response);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected VolleyError parseNetworkError(VolleyError volleyError) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return super.parseNetworkError(volleyError);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void deliverError(VolleyError error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mErrorListener.onErrorResponse(error);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}清单文件:<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; package="com.example.cachevolley" >&nbsp; &nbsp; <uses-permission android:name="android.permission.INTERNET" />&nbsp; &nbsp; <application&nbsp; &nbsp; &nbsp; &nbsp; android:allowBackup="true"&nbsp; &nbsp; &nbsp; &nbsp; android:icon="@mipmap/ic_launcher"&nbsp; &nbsp; &nbsp; &nbsp; android:label="@string/app_name"&nbsp; &nbsp; &nbsp; &nbsp; android:theme="@style/AppTheme" >&nbsp; &nbsp; &nbsp; &nbsp; <activity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:name=".MainActivity"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:label="@string/app_name" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.MAIN" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <category android:name="android.intent.category.LAUNCHER" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; </activity>&nbsp; &nbsp; </application></manifest>布局文件:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; android:paddingBottom="@dimen/activity_vertical_margin"&nbsp; &nbsp; android:paddingLeft="@dimen/activity_horizontal_margin"&nbsp; &nbsp; android:paddingRight="@dimen/activity_horizontal_margin"&nbsp; &nbsp; android:paddingTop="@dimen/activity_vertical_margin"&nbsp; &nbsp; tools:context=".MainActivity">&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/textView"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/hello_world" /></RelativeLayout>

精慕HU

如果响应是使用字符串请求从服务器获取的,则只需替换行return Response.success(new JSONObject(jsonString), cacheEntry);有了这个return Response.success(new String(jsonString), cacheEntry);在我的情况下有效。尝试使用您自己的代码。
随时随地看视频慕课网APP

相关分类

Android
我要回答