37 private RecyclerView recyclerView;
39 private List<Producto> productList;
42 public View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
43 View view = inflater.inflate(R.layout.fragment_products, container,
false);
44 recyclerView = view.findViewById(R.id.recyclerview_producto);
45 recyclerView.setLayoutManager(
new LinearLayoutManager(getContext()));
47 productList =
new ArrayList<>();
50 public void onAgregarCarritoClick(Producto producto) {
51 Toast.makeText(getContext(),
"Producto agregado al carrito", Toast.LENGTH_SHORT).show();
53 agregarProductoAlCarrito(producto.getIdProducto());
57 recyclerView.setAdapter(adapter);
61 if (getArguments() !=
null) {
62 categoriaId = getArguments().getInt(
"categoria_id", 0);
64 cargarProductos(categoriaId);
70 private void cargarProductos(
int categoriaId) {
71 String url =
"https://backmobile1.onrender.com/api/producto/";
73 StringRequest stringRequest =
new StringRequest(Request.Method.GET, url,
74 new Response.Listener<String>() {
76 public void onResponse(String response) {
79 JSONArray jsonArray = new JSONArray(response);
82 for (int i = 0; i < jsonArray.length(); i++) {
83 JSONObject jsonObject = jsonArray.getJSONObject(i);
86 int id_producto = jsonObject.getInt(
"id_producto");
87 String nombre_producto = jsonObject.getString(
"nombre_producto");
88 String descripcion = jsonObject.getString(
"descripcion");
89 double precio = jsonObject.getDouble(
"precio");
90 String imagenUrl = jsonObject.getString(
"imageURL");
91 int id_categoria = jsonObject.has(
"id_categoria") ? jsonObject.getInt(
"id_categoria") : 0;
94 if (categoriaId == 0 || id_categoria == categoriaId) {
95 Producto producto = new Producto(id_producto, nombre_producto, descripcion, precio, imagenUrl, id_categoria);
96 productList.add(producto);
101 adapter.notifyDataSetChanged();
103 } catch (JSONException e) {
105 Log.e(
"ProductsFragment",
"Error al parsear el JSON: " + e.getMessage());
108 },
new Response.ErrorListener() {
110 public void onErrorResponse(VolleyError error) {
111 Log.e(
"ProductsFragment",
"Error en la solicitud: " + error.getMessage());
112 Toast.makeText(getContext(),
"Error al cargar productos", Toast.LENGTH_SHORT).show();
117 Volley.newRequestQueue(getContext()).add(stringRequest);
120 private void agregarProductoAlCarrito(
int idProducto) {
121 String url =
"https://backmobile1.onrender.com/appCART/agregar/" + idProducto +
"/";
123 SessionManager sessionManager =
new SessionManager(getContext());
124 String token = sessionManager.getToken();
125 Log.d(
"AuthToken",
"Token usado en la solicitud: " + token);
128 StringRequest stringRequest =
new StringRequest(Request.Method.POST, url,
131 Toast.makeText(getContext(),
"Producto agregado al carrito en la base de datos", Toast.LENGTH_SHORT).show();
135 Log.e(
"ProductsFragment",
"Error al agregar al carrito: " + error.getMessage());
136 if (error.networkResponse != null) {
137 Log.e(
"ProductsFragment",
"Código de respuesta: " + error.networkResponse.statusCode);
139 Toast.makeText(getContext(),
"Error al agregar producto al carrito", Toast.LENGTH_SHORT).show();
142 public Map<String, String> getHeaders() {
143 Map<String, String> headers =
new HashMap<>();
144 headers.put(
"Authorization",
"Bearer " + token);
145 Log.d(
"HeadersDebug",
"Headers: " + headers);
150 protected Map<String, String> getParams() {
151 Map<String, String> params =
new HashMap<>();
152 params.put(
"direccion",
"casa");
153 params.put(
"cantidad",
"1");
154 Log.d(
"ParamsDebug",
"Params: " + params);
160 Volley.newRequestQueue(getContext()).add(stringRequest);
163 Toast.makeText(getContext(),
"Debes iniciar sesión para agregar productos al carrito", Toast.LENGTH_SHORT).show();