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