几天以来,我遇到了一个未知的问题,即当手机进入睡眠模式一段时间然后返回应用程序时,凌空请求变得太慢,我尝试了凌空给出的许多重试策略,但它们都没有正常工作。下面是我的示例代码请求凌空获取数据。
JsonObjectRequest movieReq = new JsonObjectRequest(
"MyURL" + session.getBusinessUserRegisterID(),
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String Connectionscount = response.getString("Connections");
String Expiredcountval = response.getString("Expired");
String Livecountval = response.getString("Live");
String NewCustomerscount = response.getString("NewCustomers");
String NewNotificationscount = response.getString("NewNotifications");
String SmsBalancecount = response.getString("SmsBalance");
String TotalCustomercount = response.getString("TotalCustomer");
Newnotificationscount.setText(NewNotificationscount);
Livecount.setText(Livecountval);
Expiredcount.setText(Expiredcountval);
Totalcustcount.setText(TotalCustomercount);
Newcustcount.setText(NewCustomerscount);
Myconnectionscount.setText(Connectionscount);
Smscount.setText(SmsBalancecount);
ringProgressDialog.dismiss();
mSwipeRefreshLayout.setRefreshing(false);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
movieReq.setRetryPolicy(
new DefaultRetryPolicy(
10000,0,1f
)
);
AppController.getInstance().addToRequestQueue(movieReq);
第一种情况,将DefaultRetryPolicy的第二个参数大于0,响应时间过长
movieReq.setRetryPolicy(
new DefaultRetryPolicy(
10000,1,1f
)
);
第二种情况将第二个参数设置为 0,因此如果 volley 10 秒内没有响应,我可能会收到错误消息(我正在显示重试警报)。
movieReq.setRetryPolicy(
new DefaultRetryPolicy(
10000,0,1f
)
);