Obtain multiple objects in PHP - JSON with Retrofit2

0

I'm trying to make a simple project of a gallery for android and I have a problem when it comes to getting the images from the server and showing them in my application since it only returns the first registered image and what I'm looking for is to show all the images are registered in the table, I'm almost sure it's because the json is badly formed but I do not know how I should fix it ...

This is the table of my DB:

And this is the part of the code where I "get" the server images

    RetrofitInterface retrofitInterface = RetrofitClient.createRetrofit().create(RetrofitInterface.class);

    Call<Imagen> call = retrofitInterface.getImagenes();

    call.enqueue(new Callback<Imagen>() {
        @Override
        public void onResponse(Call<Imagen> call, Response<Imagen> response) {

            Imagen imagen = response.body();
            Imagenes.add(imagen);
            AdaptadorImagenes adaptadorImagenes = new AdaptadorImagenes(Imagenes);
            recyclerView.setAdapter(adaptadorImagenes);

            adaptadorImagenes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
        }

        @Override
        public void onFailure(Call<Imagen> call, Throwable t) {
            Toast.makeText(getContext(), "ERROR: " + t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

This is the php:

            if(mysqli_num_rows($result) > 0){
            while($aux = mysqli_fetch_assoc($result)){
                $ruta = $aux['rutaImagen'];
                $descripcion = $aux['descImage'];
                $titulo = $aux['nomImagen'];

                $imagen = file_get_contents($ruta);
                $encodedImage = base64_encode($imagen);
                $respuesta  = json_encode(array('imgTitulo'=>$titulo,'imgDescripcion'=>$descripcion,'imgBase64'=>'$encodedImage'));
                echo $respuesta;
            }
        }

And finally this is the answer of the php (I put spaces instead of the base64 image):

{"imgTitulo":"Madoka","imgDescripcion":"Mi preciosa diosa rosada","imgBase64":""}{"imgTitulo":"Mami","imgDescripcion":"Mami Tomoe","imgBase64":""}
    
asked by Esau Gonzalez Soto 06.10.2018 в 09:17
source

1 answer

1

Your JSON is getting bad. What you should do in your PHP is:

if(mysqli_num_rows($result) > 0) {
    $respuesta = array();
    while($aux = mysqli_fetch_assoc($result)){
        $ruta = $aux['rutaImagen'];
        $descripcion = $aux['descImage'];
        $titulo = $aux['nomImagen'];
        $imagen = file_get_contents($ruta);
        $encodedImage = base64_encode($imagen);
        $respuesta[] = array('imgTitulo'=>$titulo,'imgDescripcion'=>$descripcion,'imgBase64'=>'$encodedImage');
    }
    echo json_encode($respuesta);
}

This way you do not print the elements one after the other, but you put them in an array and when you finish them you convert the array into JSON and print it.

    
answered by 06.10.2018 / 14:54
source