gpt4 book ai didi

Android Volley DefaultRetryPolicy 无法按预期工作

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

所以,我有这个 Volley PUT 请求:

private boolean syncCall(JSONObject jsonObject, final VolleyCallback
callback) {

final ProgressDialog progDailog = new ProgressDialog(context);

final Boolean[] success = {false};

progDailog.setMessage("...");
progDailog.setIndeterminate(false);
progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

progDailog.setCancelable(false);
progDailog.show();

final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);

RequestQueue queue = Volley.newRequestQueue(context, new HurlStack());

final String token = prefs.getString("token", null);

String URL = Constants.getUrlSync();
String param1 = String.valueOf(prefs.getInt("pmp", 1));
String param2 = String.valueOf(prefs.getInt("ei", 1));

URL = URL.replace("[x]", param1);
URL = URL.replace("[y]", param2);

//pegar id pmp e IE corretas
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request
.Method.PUT, URL, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
callback.onSuccess(response + "");
success[0] = true;
progDailog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

callback.onFailure(error);
tokenFailure(error);
success[0] = false;
progDailog.dismiss();
}
}) {


@Override
public Map<String, String> getHeaders() throws
AuthFailureError {

HashMap<String, String> headers = new HashMap<>();
headers.put("Token", token);

return headers;
}
};

int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

jsObjRequest.setRetryPolicy(policy);


queue.add(jsObjRequest);

return success[0];
}

我的问题是我发送了一个非常大的 JSON,所以默认的 5 秒超时是不够的。因此,我尝试将超时增加到 30 秒,并使用 DefaultRetryPolicy 来增加重试次数。

问题是,它会在 5 秒内保持超时,甚至不会重试一次!

重试时我必须要有监听器或回调吗?我对 DefaultRetryPolicy 做错了什么?请帮忙,这个问题让我抓狂......

最佳答案

是否需要使用 DefaultRetryPolicy?

因为您可以定义自己的。

取而代之的是:

RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,     
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

试试这个:

jsObjRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
// Here goes the new timeout
return mySeconds;
}
@Override
public int getCurrentRetryCount() {
// The max number of attempts
return myAttempts;
}
@Override
public void retry(VolleyError error) throws VolleyError {
// Here you could check if the retry count has gotten
// To the max number, and if so, send a VolleyError msg
// or something
}
});

关于Android Volley DefaultRetryPolicy 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35506237/

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