仅第一次来自 Volley 的响应

Response coming from Volley the first time only

我正在使用 Volley 从服务器获取数据。我有 2 个活动,Activity A 和 B。两者都使用 Volley 和相同的请求队列,通过 Singleton 来获取数据。在 Activity A 中一切正常,当我开始 Activity B 时,我得到了 Volley 的响应。

问题是,如果我从activity B 结束然后移动到A,然后再开始B,Volley 似乎没有得到响应。我做错了什么?

我的单身狗

public class CustomVolleyRequestQueue {

    private static CustomVolleyRequestQueue mInstance;
    private static Context mCtx;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private CustomVolleyRequestQueue(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();
        mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache(
                LruBitmapCache.getCacheSize(mCtx)));
    }

    public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new CustomVolleyRequestQueue(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
            Network network = new BasicNetwork(new HurlStack());
            mRequestQueue = new RequestQueue(cache, network);
            // Don't forget to start the volley request queue
            mRequestQueue.start();
        }
        return mRequestQueue;
    }

    public ImageLoader getmImageLoader(){
        return mImageLoader;
    }

}

我的自定义请求

public class CustomJSONObjectRequest extends JsonObjectRequest{

    private Priority mPriority;

    public CustomJSONObjectRequest(int method, String url, JSONObject jsonRequest,
                                   Response.Listener<JSONObject> listener,
                                   Response.ErrorListener errorListener) {
        super(method, url, jsonRequest, listener, errorListener);
        //this.setShouldCache(true);
    }


    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        return headers;
    }

    @Override
    public RetryPolicy getRetryPolicy() {
        // here you can write a custom retry policy
        return super.getRetryPolicy();
    }

    public void setPriority(Priority priority) {
        mPriority = priority;
    }

    @Override
    public Priority getPriority() {
        return mPriority == null ? Priority.NORMAL : mPriority;
    }

}

我在 Activity B 的 onStart 上执行并添加请求如下,

  protected void onStart() {
        super.onStart();
        mQueue = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
                .getRequestQueue();

        final CustomJSONObjectRequest jsonRequest = new CustomJSONObjectRequest(Request.Method
                .GET, url,
                new JSONObject(), this, this);

        jsonRequest.setTag(REQUEST_TAG);
        jsonRequest.setPriority(Request.Priority.HIGH);
        mQueue.add(jsonRequest);
        setupRecyclerView(rv, rv2, rv3);
    }

我的 activity B 实现了响应侦听器,我在其中简单地解析 JSON 并在 UI 上显示数据。

我已经研究这个问题很长时间了,我已经了解了 Volley 其他功能的提示和技巧,缓存,忽略请求,深入研究库并将其与其他图书馆。然而,我仍然看不出我在这里做错了什么。

好吧找到了我的解决方案,这很尴尬。我正在使用 static URL 添加 GET 参数。因为是第一次用Volley,不知道怎么给requests加GET参数,等会再说吧。我忘了。

所以基本上我在下次移动到下一个 activity 时连接 GET 参数值,因此从服务器得到一个空响应,因为那里不存在这些值。

太简单没注意到,我应该更仔细地调试。