当我的Volley响应中有问题(错误响应,JSON格式不正确等)时,我在5分钟后重试了该请求。但是,如果我有5或6次重试(不确定实际次数),Volley将停止向服务器发送请求。
a。)这应该做吗?
b。)如果是这样,我可以更改嵌套重试的次数吗?
static void sendMessage(final String message, final Context context) {
RequestQueue MyRequestQueue = Volley.newRequestQueue(context);
StringRequest MyStringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
processResponse(jsonObject);
}
} catch (Exception e) {
Runnable r = new Runnable() {
@Override
public void run() {
sendMessage(message, context);
}
};
Handler h = new Handler();
h.postDelayed(r, 5 * 60 * 1000);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Runnable r = new Runnable() {
@Override
public void run() {
sendMessage(message, context);
}
};
Handler h = new Handler();
h.postDelayed(r, 5 * 60 * 1000);
}
}) {
protected Map<String, String> getParams() {
final Map<String, String> params = new HashMap<>();
params.put("MESSAGE", message);
return params;
}
};
MyRequestQueue.add(MyStringRequest);
}