gpt4 book ai didi

php - 读取 MySQL blob 字段并转换为 Android Webservice 的 JSON

转载 作者:行者123 更新时间:2023-11-29 16:34:11 28 4
gpt4 key购买 nike

我正在为 Android 创建一个 Web 服务,其中我必须从 MySQL 数据库读取 blob 字段。问题是我无法将该字段编码为 JSON,这是我稍后读取 Android Web 服务时需要的格式。

让我们看看是否有人知道我做错了什么。这是我使用 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);


?>

我尝试将其转换为 char,而不转换它...事实是,如果我使用 print_r 打印,我会在屏幕上得到数据,但是当我将其编码为 JSON 时,我什么也得不到。

这就是我从 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);
}

Android 返回此错误:

W/System.err: org.json.JSONException: End of input at character 0 of

但我不知道问题是来自webservice还是来自android

欢迎任何帮助。谢谢

最佳答案

我对自己回答,因为经过多次尝试我找到了一个可以接受的解决方案。该错误出现在 SQL 语句中,该语句必须事先转换为一组字符,在本例中为 Latin1。它看起来像这样:

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

json返回的内容可以识别html字符和标签。例如,只需使用 WebView 读取 Android 上的 Web 服务即可识别 Base64 中的奇怪字符和图像。

关于php - 读取 MySQL blob 字段并转换为 Android Webservice 的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53711439/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com