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