Android Volley库通过蜂窝数据连接提供超时错误

时间:2016-01-29 19:53:46

标签: java android android-volley

即时通讯使用Android排球库为我的应用程序和当我使用wifi工作但它似乎有蜂窝数据连接的问题。在一些手机(LG G3 Android 6,魅族Android 5.1)与蜂窝数据它不起作用,并给出超时错误但在(HTC欲望816 android 4.4,LG g3安卓5.0)再次其工作

问题是什么?

logcat的:

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

1 个答案:

答案 0 :(得分:17)

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

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