Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley library gives timeout error with cellular data connection

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);
like image 521
Amir_P Avatar asked Apr 25 '26 01:04

Amir_P


1 Answers

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);
like image 90
Msp Avatar answered Apr 27 '26 15:04

Msp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!