返回使用JSON一个blob [英] Returning a blob with json

查看:2079
本文介绍了返回使用JSON一个blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造我的Andr​​oid应用程序休息服务,而外部数据库返回到存储在应用程序的本地数据库项目。我已经得到了一切工作,除了斑点正在返回空值。

这是我的贾森响应的例子。(图片和缩略图字段的斑点)

<$p$p><$c$c>{"id":"2","user_id":"1","name":"testing","type":"bouldering","picture":null,"lat":"36","long":"81","alt":"41932","accuracy":"53","thumbnail":null}

下面是我的PHP脚本返回的数据。

 &LT; PHP
require_once('config.php文件');

$库MySQLi =新库MySQLi(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$ SQL =SELECT * FROM spot_main';
$结果= $ mysqli-&GT;查询($ SQL);

$点=阵列(); //数组从解析贾森

而($现货= $结果&GT; FETCH_ASSOC()){
    $点[] = $点;
}

回声json_en code($点);
?&GT;
 

任何人都知道解决这个问题的?我知道我不这样做的最有效的方式(最好是存储在文件系统中的图像),但我需要这个工作。

解决方案

恩code生成的JSON前的二进制数据为base64。

  $ obj-&GT;图片= base64_en code($ binaryData);
 

您可以在你的Andr​​oid应用程序与任何基地64日codeR然后去code这一点。由于API级别8有一个内置的 UTIL类。否则,有大量的外部库可用于目标定位的Andr​​oid 2.1或更早。

I'm trying to create a rest service for my android application where an external database returns items to be stored in the applications local database. I've got everything working except blobs are being returned as null.

this is an example of my jason response.(picture and thumbnail fields are blobs)

{"id":"2","user_id":"1","name":"testing","type":"bouldering","picture":null,"lat":"36","long":"81","alt":"41932","accuracy":"53","thumbnail":null}

Here is my php script to return the data.

<?php
require_once('config.php');

$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$sql = 'select * from spot_main';
$results =$mysqli->query($sql);

$spots = array();  //array to parse jason from

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

echo json_encode($spots);
?>

Anyone know of a solution to this problem? I know I'm not doing this the most efficient way(better to store images in the filesystem), but I need this to work.

解决方案

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.

这篇关于返回使用JSON一个blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆