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

时间:2015-05-28 09:37:35

标签: 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>

当我提出:&#34;&#39; pic&#39; =&gt; $ pic =($ car-&gt; getPic());&#34;在评论中,他收到所有没有图像的汽车。

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

PS我花了3天时间试图解决这个问题,但没有得到任何结果。我尝试使用这个http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html但是没有用。

1 个答案:

答案 0 :(得分:0)

您需要在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的功能。

希望这会有所帮助......