Search code examples
javaandroidandroid-studioandroid-volleyandroid-studio-3.0

Volley request timeout


Help me guys please sometimes the code is running and connect faster to the website PHP but sometimes it show me volley Request Timeout, is there something wrong with my code.

when I run this in my localhost its fast but when we access the PHP in the website it take too long and sometimes give me volley request timeout, is there a way to make it faster?

I am using Hostinger as webhost.

findViewById(R.id.sendotp).setOnClickListener(view -> {
            Toast.makeText(getApplicationContext(), "Sending Please Wait", 
            Toast.LENGTH_SHORT).show();
            Random random = new Random();
            code = random.nextInt(899999) + 100000;
            String url = "https://mywebsite.com/myphp_php/myphp.php";
            RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                    response -> Toast.makeText(MainActivity.this, ""
                            + response, Toast.LENGTH_SHORT).show(), error ->
                    Toast.makeText(MainActivity.this, "" + error,
                            Toast.LENGTH_SHORT).show()) {
                @Nullable
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> mydata = new HashMap<>();
                    mydata.put("email", editTextEmail.getText().toString());
                    mydata.put("code", String.valueOf(code));
                    return mydata;
                }
            };
            //stringRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 2, 
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            requestQueue.add(stringRequest);
        });

Solution

  • Try this code before requestQueue.add(stringRequest); line:

    stringRequest.setRetryPolicy(new DefaultRetryPolicy(0,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES
                , DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));