gpt4 book ai didi

android - Volley 使用 StringRequest 不调用 getParams 以在第一次后发送 POST 请求参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:10:27 24 4
gpt4 key购买 nike

我遇到了一个问题,我的 POST 请求参数在第一次后无法发送到服务器。我知道 Volley 使用缓存机制进行响应,但在我的例子中,我的请求参数值可以在运行时更改,因为我在 Recyclerview 中使用分页。

所以我的问题是如何每次都发送 Post 请求参数并且不会丢失 volley 的缓存机制。

我已经尝试使用下面的方法来完成我的工作(每次调用 getParams())..但是它会丢失缓存响应,我不希望这样。

requestQueue.getCache().clear();

stringRequest.setShouldCache(false);

也搜索了谷歌和下面的链接,但找不到任何合适的解决方案。以下是 SO 链接

下面是我的代码:

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {

Log.e("RES", response);


GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("M/d/yy hh:mm a"); //Format of our JSON dates
Gson gson = gsonBuilder.create();

NewsFeedPOJO resultObj = (NewsFeedPOJO) gson.fromJson(response, (Class) NewsFeedPOJO.class);

inCurrPage = Integer.parseInt(resultObj.getPagination().getCurrent_page());
inTotalPage = Integer.parseInt(resultObj.getPagination().getTotal_pages());
inCurrPage++;

arrayList.addAll(resultObj.getNewsFeedList());
if (isFtym) {
isFtym = false;
layoutManager = new LinearLayoutManager(MainActivity.this);
rcNewsFeed.setLayoutManager(layoutManager);
adapter = new NewsFeedAdapter(MainActivity.this, arrayList);
rcNewsFeed.setAdapter(adapter);
} else {
adapter.notifyItemInserted(arrayList.size());
adapter.notifyDataSetChanged();
}

}

}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put("user_id", "188");

if (inCurrPage == 0)
map.put("page", "1");
else {
map.put("page", "" + inCurrPage);
}

Log.e("RES", inCurrPage + " PARA");
return map;
}
};
//RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
//requestQueue.add(stringRequest);
//requestQueue.getCache().clear();

//AppController.getInstance().addToRequestQueue(stringRequest);
// stringRequest.setShouldCache(false);
VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);

使用下面的 Volley 依赖项。

compile 'com.android.volley:volley:1.1.0'

如果需要更多信息,请告诉我。提前致谢。我们将不胜感激。

最佳答案

你检查过你的 Volley Singleton 了吗?

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

public class VolleySingleton {
private static AppSingleton mAppSingletonInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mContext;

private AppSingleton(Context context) {
mContext = context;
mRequestQueue = getRequestQueue();

mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);

@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}

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

public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
}
return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req,String tag) {
req.setTag(tag);
getRequestQueue().add(req);
}

public ImageLoader getImageLoader() {
return mImageLoader;
}

public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}

或者您的代码中可能存在其他问题...

关于android - Volley 使用 StringRequest 不调用 getParams 以在第一次后发送 POST 请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48126788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com