【问题标题】:Android Volley library gives timeout error with cellular data connectionAndroid Volley 库在蜂窝数据连接时出现超时错误
【发布时间】:2016-05-07 16:24:14
【问题描述】:

我在我的应用程序中使用 android volley 库,当我使用 wifi 时,它可以正常工作,但蜂窝数据连接似乎有问题。在某些带有蜂窝数据的手机(LG G3 Android 6,魅族 android 5.1)中它不起作用并给出超时错误,但在(HTC 渴望 816 android 4.4,lg g3 android 5.0)中它再次工作

有什么问题?

日志猫:

D/ERROR: error => com.android.volley.TimeoutError

代码:

showLoadingDialog();
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jobj = new JSONObject(response);
                    JSONObject jobjHRM = jobj.getJSONObject("HRM");
                    int status = jobjHRM.getInt("StatusCode");
                    if (status == 200) {
                        if (jobj.getJSONObject("Data").getBoolean("result") == true) {
                            login_info_editor.putInt("id", jobj.getJSONObject("Data").getInt("id"));
                            login_info_editor.putString("username", txtlayoutid.getEditText().getText().toString());
                                login_info_editor.putString("password", txtlayoutpass.getEditText().getText().toString());
                            login_info_editor.putBoolean("loged", true);
                            login_info_editor.commit();
                            checkversion();
                        } else {
                            login_info_editor.clear();
                            login_info_editor.commit();
                            txtlayoutid.requestFocus();
                            Toast.makeText(getApplicationContext(), "نام کاربری یا کلمه ی عبور اشتباه است", Toast.LENGTH_LONG).show();
                            txtlayoutid.startAnimation(shake);
                            txtlayoutpass.startAnimation(shake);
                            hideLoadingDialog();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "ارور " + status, Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    hideLoadingDialog();
                    Log.d("ERROR", "error => " + error.toString());
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put(xxxxxx);
            params.put(xxxxxx);
            params.put(xxxxxx);
            return params;
        }

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded; charset=UTF-8";
        }
    };
    AppController.getInstance().addToRequestQueue(postRequest);

【问题讨论】:

    标签: java android android-volley


    【解决方案1】:

    尝试配置重试策略。给一个大的超时并尝试,

    int socketTimeout = 30000; // 30 seconds. You can change it
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,                 
    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,     
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    
    postRequest.setRetryPolicy(policy);
    AppController.getInstance().addToRequestQueue(postRequest);
    

    【讨论】:

    • socketTimeout尝试不同的值
    • 因为当时postRequest 为空。在AppController.getInstance().addToRequestQueue(postRequest)之前添加它
    • 太棒了。快乐编码:)
    猜你喜欢
    • 2019-10-07
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多