php - 将 json 文件中的 blob 数据发送到 android 应用程序

标签 php android sql json symfony

我正在尝试将 json 文件中的图像发送到 android 应用,这是我的代码:

public function allUserCarsAction()
    {
        $request = Request::createFromGlobals();
        $UId = $request->request->get('UId');

        $em = $this -> getDoctrine();
        $id = $em -> getRepository("OBCarsTest2Bundle:user") ->findOneById($UId);
        $UserCars = $em -> getRepository("OBCarsTest2Bundle:cars") ->findByidUser($id);

        $a = 0;

        if($UserCars != Null)
        {
            foreach($UserCars as $car)
            {
                $ModelId = $em -> getRepository("OBCarsTest2Bundle:models")->findOneById($car->getModels());
                $BrandsId = $em -> getRepository("OBCarsTest2Bundle:brands")->findOneById($car->getBrands());

                if($ModelId!=Null && $BrandsId != Null)
                {
                    $infoCars = array ('name'=>$BrandsId->getCarBrand(),
                               'model'=>$ModelId->getCarModel(),
                               'carvin'=>$car->getVin(),
                               'price'=>$car->getPrice(),
                               'currency'=>$car->getCurrency(),
                               'pic'=>$pic=($car->getPic()));

                    $userCar[$a] = $infoCars;
                    $a++;
                }
            }
            $Cars = array ('cars'=>$userCar);
            $CarsSent = json_encode($Cars);
        }
        else
            $CarsSent = '{"CarsSent":0}';

        return new Response($CarsSent);
    }

也就是配置图片的实体:

     /**
     * @var blob
     *
     * @ORM\Column(name="$pic", type="blob")
     */
    private $pic;

    /**
     * Set pic
     *
     * @param string $pic
     * @return cars
     */
    public function setPic($pic)
    {
        $this->pic = $pic;

        return $this;
    }

    /**
     * Get pic
     *
     * @return string 
     */
    public function getPic()
    {
        return $this->pic;
    }

但是我的合作伙伴在尝试获得响应时收到此消息:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>An Error Occurred: Internal Server Error</title>
    </head>
    <body>
        <h1>Oops! An Error Occurred</h1>
        <h2>The server returned a "500 Internal Server Error".</h2>

        <div>
            Something is broken. Please let us know what you were doing when this error occurred.
            We will fix it as soon as possible. Sorry for any inconvenience caused.
        </div>
    </body>
</html>

当我输入:"'pic'=>$pic=($car->getPic()));"在评论中,他收到了所有没有图像的汽车。

如何在 json 文件中发送图像?

PS 我花了 3 天时间试图解决这个问题,但没有取得任何进展。我试着用这个 http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html但没有用。

最佳答案

您需要使用 base64 对其进行编码。

鉴于 $car->getPic() 保存实际的二进制数据,您需要:

$pic=$car->getPic();
$infoCars = array (
    'name'=>$BrandsId->getCarBrand(),
   'model'=>$ModelId->getCarModel(),
   'carvin'=>$car->getVin(),
   'price'=>$car->getPrice(),
   'currency'=>$car->getCurrency(),
   'pic'=> base64_encode($pic) // <-- THIS
);

根据图像的用途,另一方需要解码。我知道 CSS 允许将 background-image 设置为 base64 编码值,但是,我不熟悉 Android 的功能。

希望这有助于...

关于php - 将 json 文件中的 blob 数据发送到 android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30502736/

相关文章:

PHP 权限错误 - 我需要执行权限?

php - 如何添加到 Azure Web 服务上的 PHP.ini?

php - 在 PHP 中创建链接

android为方向提供不同的drawable资源

java - 提高发送java邮件的速度

sql - 对于连接表中所有缺失的组合返回 0

php - 如何创建一个从代码中获取参数并返回字符串的函数?

android - 出现错误 : inconsistent operand constraints in an 'asm' while porting my c++ code from linux to android

android - clearTaskOnLaunch 如何操作?

mysql - 在同一表中复制数据时排除特定行的插入