4 votes

Volley TimeOutError. Android Studio

I happen to be working with volley. When I want to fill a Recyclerview I get the error described in the title, I've been reading and I know it's because of the response time between the application and the server (in this case), I tried different solutions that ended up affecting the project (for example I was closed). If you need any other part of the code let me know, although this should be enough because I know that this part of the code is used to implement the timing.

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

    }

0 votes

I see that you make the request locally, do you have apache enabled so that Volley can connect to your Web-Service?

7voto

A. Cedano Points 48884

I remember having a similar problem in the past and I solved it as follows:

  1. Declaring a constant MY_DEFAULT_TIMEOUT

In my case, this constant is in my class Constants which I then import wherever I need them. I have given it a value of 15000 :

public class Constants {

    public static final int MY_DEFAULT_TIMEOUT = 15000;

    //...
}
  1. Establish a retry policy in my Volley request using the setRetryPolicy 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 any more timeout problems.

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

If all is well on that side and it still doesn't 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 the database. Sometimes responses are slow because the PHP code is not optimized, or because the indexes in the database are not the right ones, etc.

1 votes

Thank you very much friend !!!!! It served me perfectly and very well explained by the way, I would give you many likes but I can only give you one hahaha, I'm really very grateful! :D

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X