Volley TimeOutError. Android Studio

3

I happen to be working with volleyball. When I want to fill a Recyclerview it turns out that I see the error described in the title, I was reading and I know that it is because of the response time between the application and server (in this case), I tried different solutions that ultimately ended up affecting the project ( for example they closed me). If they need any other part of the code, they let me know, although this should be enough because I know that this part of the code is used to implement the times.

JsonObjectRequest jsonObjectRequest;

        listaProducto = new ArrayList<>();

        recyclerProducto = (RecyclerView) vista.findViewById(R.id.listaTodasRec);
        recyclerProducto.setLayoutManager(new LinearLayoutManager(this.getContext()));
        recyclerProducto.setHasFixedSize(true);

        request = Volley.newRequestQueue(getContext());

        cargarWebService();

        return vista;
    }

    private void cargarWebService() {
        progress = new ProgressDialog(getContext());
        progress.setMessage("Buscando productos..");
        progress.show();

        String url = "http://192.169.1.40:7777/Proyecto/buscartodas.php";

        jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url,null,this,this);
        request.add(jsonObjectRequest);
    }


    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getContext(), "No se puede conectar, error: "+error.toString(), Toast.LENGTH_LONG).show();
        System.out.println();
        Log.d("Error: ", error.toString());
        progress.hide();
    }


    @Override
    public void onResponse(JSONObject response) {
        Productos productos= null;
        JSONArray json = response.optJSONArray("productos");

        try{
            for (int i = 0; i<json.length();i++)
            {
                productos= new Productos();
                JSONObject jsonObject = null;
                jsonObject = json.getJSONObject(i);

                productos.setIdProducto(jsonObject.optInt("id_prod"));
                productos.setNombreProducto(jsonObject.optString("nomb_prod"));
                productos.setPieProducto(jsonObject.optString("pie_prod"));

                listaProductos.add(productos);
            }
            progress.hide();
            ProductosAdapters adapters = new ProductosAdapters (listaProductos);
            recyclerProducto.setAdapter(adapters);
        }
        catch (JSONException e){
            e.printStackTrace();
            Toast.makeText(getContext(), "No se ha podido conectar con el servidor" + " " + response, Toast.LENGTH_LONG).show();
            progress.hide();
        }

    }
    
asked by alexander 18.07.2018 в 02:06
source

1 answer

3

I remember having a similar problem in the past and I resolved it in the following way:

  • Declaring a constant MY_DEFAULT_TIMEOUT
  • In my case, this constant is in my class Constants which I import then where you need them. I gave it a value of 15000 :

    public class Constants {
    
        public static final int MY_DEFAULT_TIMEOUT = 15000;
    
        //...
    }
    
  • Establish a retry policy in my Volley request using the setRetryPolicy method in which I use my constant.

    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
            MY_DEFAULT_TIMEOUT,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    
  • This should be done before adding the request to the queue , that is, before this line:

        request.add(jsonObjectRequest);
    

    Since then I have not had timeout problems again.

    If it still does not work, you should verify that there is no problem with the URL http://192.169.1.40:7777/Proyecto/buscartodas.php , testing it from the browser for example or checking the PHP error log.

    If all is well on that side and still does not work, consider increasing the value of the constant, at least for requests that are too long. Or ... consider optimizing the code on the server or database. Sometimes the answers are slow because the PHP code is not optimized, or because the indexes in the database are not adequate, etc.

        
    answered by 18.07.2018 / 06:48
    source