Read MySQL blob field and convert to JSON for android webservice

0

I am creating a webservice for Android, in which I have to read a blob field from a MySQL database. The problem is that I can not encode that field to JSON, which is the format I will need later to read the Android webservice.

Let's see if anyone knows what I'm doing wrong. This is my webservice with PHP:

<?php 

require('../../conect/conect.php');


$dbh -> exec("set names utf8");


$res=$dbh->query("select cast(mensaje as char(10000) character set utf8) mensa from ges_mensajes where cod_destino=4;");


$datos = array();

foreach ($res as $row) {

    $datos[] = $row;

}

//print_r($datos);  //Si ejecuto esta línea me sale bien por pantalla

echo json_encode($datos);


?>

I tried to cast it to char, without casting it ... the fact is that if I print with print_r I get the data on the screen, but when I encode it to JSON, I do not get anything.

And that's how I read it from Android:

public void ObtMensaje_volley(){

       ArrayList<String> mensaje = new ArrayList<>();
        String url = "https://local.es/WS_neton/obt_mensaje_blob.php";

        StringRequest eventfulRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {

                            JSONArray jsonArray = new JSONArray(response);


                            String mensajeM;


                            for (int i=0; i<jsonArray.length(); i++){


                                mensajeM = jsonArray.getJSONObject(i).getString("mensa");
                                mensaje.add(mensajeM);

                            //Toast.makeText(getApplicationContext(), "mensaje: "+String.valueOf(mensajeM), Toast.LENGTH_SHORT).show();

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //Log.e("Error: ", error.toString());
                    }
                });

        VolleySingleton.getInstance(this)
                .addToRequestQueue(eventfulRequest);
    }
    
asked by Mimmetico 10.12.2018 в 17:42
source

1 answer

1

I autorespond to myself, because after many laps I found an acceptable solution. The error is in the SQL statement, which must previously be cast to a character set, in this case Latin 1. It would look like this:

$res=$dbh->query("select CONVERT(CAST(mensaje as BINARY) USING latin1) as mensa from ges_mensajes where cod_destino=4;");

What returns a json capable of recognizing html characters and tags. It only remains to read the webservice in android with a WebView, for example, which will recognize strange characters and images in base64.

    
answered by 11.12.2018 в 11:45