Returning a blob with json

北慕城南 提交于 2019-11-28 00:55:39

Encode the binary data as base64 before you generate the JSON.

$obj->picture = base64_encode($binaryData);

You can then decode this in your Android application with any base 64 decoder. Since API level 8 there is a built in util class. Otherwise there are plenty of external libs you can use for targetting Android 2.1 or earlier.

you have to make BLOB to base64_encode

 while($spot = $results->fetch_assoc()){
     $spots[] = $spot;                                                           
    }

Then prepare a foreach loop like this

foreach($spots as $key=>$value){
    $newArrData[$key] =  $spots[$key];
    $newArrData[$key]['picture'] = base64_encode($spots[$key]['picture']);
    $newArrData[$key]['thumbnail'] = base64_encode($spots[$key]['thumbnail']);
}
header('Content-type: application/json');
echo json_encode($newArrData);

It seems that json_encode works with UTF-8 encoded data only. You can use json_last_error() to detect json_encode error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!