- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我遇到了一个问题,我的 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/
我想使用 Volley StringRequest 将 base64 编码的图像和一些文本上传到我的网络服务器。 当我直接尝试发送 base64 字符串时,我得到“意外响应代码 503”,但是当我压缩
我有一些从谷歌应用程序脚本接收数据的方法,但为所有方法放置相同的代码有点长。 例如,这是我的 checkIn 方法,但我需要创建 checkOut 方法或 UpdateReservation 来从 g
我正在使用 android volley ,我想用 stringRequest 发送发布数据。但参数不发送到服务器。这是我的代码: StringRequest jsonObjReq =
我是 Android 的新手,我希望得到有关 StringRequestVolley 的帮助。在我的项目中,我将调用多个 API。为此,我创建了一个名为 ProcessAPIRequest 的方法。为
我正在尝试使用 Volley 发送请求,但我不知道如何让它工作。 我需要发送一个以 JSON 编码数据作为正文的 POST 请求,但在尝试了数小时不同的方法后,我仍然无法让它工作。 这是我当前的请求代
我对应该使用哪个感到有点困惑。 尽管其响应是 JSON 字符串,但该 API 试图接受常规文本发布参数。 我应该使用哪个? 最佳答案 为此,您可以使用 JSON 对象请求。 关于android - 我
我用过this简单的获取请求代码 final StringBuilder result = new StringBuilder(); RequestQueue queue = Voll
我正在创建一个按钮,单击该按钮会调用 checkUserName 方法,该方法会搜索数据库并告知用户名是否可用。 URL_CHECK_USERNAME 响应“用户名可用”或“用户名不可用”。
我已经放置了这个依赖项使用 volley 并从受尊重的 URl 获取 JSON 数据:- **compile 'me.neavo:volley:2014.12.09'** 放置此后没有得到 Strin
我正在尝试从 Android 发出 POST 请求以在我的 phpmyadmin 中插入一些信息。 我在用什么 Slim 与存储在 phpmyadmin 的数据库建立连接和查询。 XAMPP 模拟本地
我的 Volley 代码曾经像这样正常工作: StringRequest stringRequest = new StringRequest(method, URL, listener, errorL
下面的代码将实现一个简单的在线登录系统。 php 脚本路径在常量 authenticationURL 中定义(MainActivity.java 中的第 34 行)。它已经过 Postman 测试,可
api 调用发送了正确的响应,但是当我从 Volley StringRequest 得到响应时,撇号符号更改为不可读的字符。 我用来进行 api 调用的代码是这样的: private fun make
我已经能够使用 volley 从 url 获取字符串。该字符串现在在 TextView 中显示为一个 block 。但我希望能够在单独的 TextView 中显示这些数据。我怎样才能做到这一点? 也许
我正在使用以下代码在我服务器上的 sql 数据库中注册用户。 package com.cc.envoycc.activity; import android.app.Activity; import
我目前正在 fragment 内处理 ListView 。它工作得很好,但我需要使用用户的用户名过滤 ListView 上的数据。我如何能够使用请求传递“用户名”?任何帮助、建议和提示将不胜感激。预先
我试图通过在 Android 中使用 StringRequest 以及 POST 方法来获取 JSONArray。 php脚本的写法如下: $statement = mysqli_prepare($c
我有以下代码可以调用和接收 xml。 StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
当我在调试器中运行该方法时,数据按预期显示,但是每当我尝试使用它执行任何操作时,parentObject 的值都会返回为 null。 我只是想从服务器获取响应并将其存储为 JSONObject 以便在
我正在使用 Android Volley 进行网络通话。一般我使用JSONRequest接收json数据,然后使用GSON将它们转换成对象。 new Response.Listener() {
我是一名优秀的程序员,十分优秀!