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

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

本文介绍了Android Volley库提供蜂窝数据连接超时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了android volley库,而当我使用wifi时,它却可以正常工作,但蜂窝数据连接似乎有问题.在某些具有蜂窝数据的手机(LG G3 Android 6,魅族android 5.1)中,它无法正常工作并给出超时错误,但在(HTC要求816 android 4.4,lg g3 android 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);
解决方案

尝试配置重试策略.设置较大的超时时间并尝试

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

im using android volley library for my application and when im using wifi its working but it seems to have problem with cellular data connection. in some phones(LG G3 Android 6,Meizu android 5.1) with cellular data it doesnt work and gives timeout error but in (HTC desire 816 android 4.4, lg g3 android 5.0) again its working

what is the problem?

logcat:

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

code:

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);
解决方案

Try to configure retry policy. Give a large timeout and try,

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

这篇关于Android Volley库提供蜂窝数据连接超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:44