我正在使用自定义重试策略的截击发出帖子请求。
套接字超时为30000毫秒。我的服务器正在接收发布请求,但是由于某种原因凌空在30秒之前抛出超时错误。
我的服务器加载时间约为5500毫秒,包括延迟。 我的自定义重试策略如下所示:
RETRY POLICY
public RetryPolicy getGembaRetryPolicy(){
DefaultRetryPolicy defaultRetryPolicy = new DefaultRetryPolicy(
30000,
0,
1f);
return defaultRetryPolicy;
}
30000是SOCKET_TIMEOUT_IN_MS
0是RETRIES
1f BACKOFF_MULT
CUSTOM REQUEST CLASS:
public class CustomJSONObjectRequest extends JsonObjectRequest {
public String authKey = null;
private Map<String, String> params;
public CustomJSONObjectRequest(int method, String url, JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener, String authKey) {
super(method, url, jsonRequest, listener, errorListener);
this.authKey = authKey;
}
protected Map<String, String> getParams()
throws AuthFailureError {
return params;
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError){
if(volleyError.networkResponse != null && volleyError.networkResponse.data != null){
VolleyError error = new VolleyError(new String(volleyError.networkResponse.data) + authKey);
volleyError = error;
}
return volleyError;
}
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
if (authKey != null) {
headers.put("Authorization", "Token " + authKey);
Log.d("AUTH_KEY:", authKey);
}
return headers;
}
@Override
public RetryPolicy getRetryPolicy() {
return AppInstance.getInstance().getGembaRetryPolicy();
}
}
APPINSTANCE:
public class AppInstance extends MultiDexApplication {
public static Typeface RobotoRegular;
public static Typeface RobotoLight;
private static AppInstance instance;
public static AppInstance getInstance() {
return instance;
}
@Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
instance = this;
RobotoRegular = Typeface.create("sans-serif", Typeface.NORMAL);
RobotoLight=Typeface.create("sans-serif-light", Typeface.NORMAL);
RealmManager.getInstance(getApplicationContext());
Realm realm = Realm.getInstance(getApplicationContext());
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
public RetryPolicy getGembaRetryPolicy(){
DefaultRetryPolicy defaultRetryPolicy = new DefaultRetryPolicy(
IBotConstants.SOCKET_TIMEOUT_IN_MS,
IBotConstants.RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
return defaultRetryPolicy;
}
}
最后,我将请求添加到队列中:
jsonRequest = new CustomJSONObjectRequest(method, url, json, this, this, authKey);
mQueue.add(jsonRequest);