【问题标题】:Volley Request becomes too slow when phone goes to sleep and come back again to Application当手机进入睡眠状态并再次返回应用程序时,Volley Request 变得太慢
【发布时间】:2017-03-24 14:54:08
【问题描述】:

我面临几天以来未确定的问题,即当手机进入睡眠模式一段时间然后返回应用程序时,凌空请求变得太慢,我尝试了凌空给出的许多重试策略,但没有一个能正常工作.below 是我请求 volley 获取数据的示例代码。

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
                    )
            );

【问题讨论】:

  • 尝试给予优先级 = HIGH
  • 感谢@BasuSingh 的回复。什么是优先级,我在哪里可以提供?
  • 我把代码写成了答案。

标签: android android-volley retrypolicy


【解决方案1】:

尝试将优先级设置为高:

@Override
public Priority getPriority() {
       return Priority.HIGH;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多