【问题标题】:sending blob data in json file to android app将 json 文件中的 blob 数据发送到 android 应用程序
【发布时间】:2015-05-28 09:37:35
【问题描述】:

我正在尝试将 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 但没有用。

【问题讨论】:

    标签: php android sql json symfony


    【解决方案1】:

    您需要将其编码为base64

    鉴于$car-&gt;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 的功能。

    希望这会有所帮助...

    【讨论】:

    • 我正在对其进行编码并按原样保存。是不是我保存图片的方式不对?
    • 澄清一下:数据是在Symfony2 -&gt; Android 还是相反?
    • 那么,$car-&gt;getPic() 是否返回预期数据?尝试使用浏览器调用此操作并 echo-int $pic...的内容...
    • 嘿达鲁姆,我看到你接受了答案。您能告诉我们您是如何解决这个问题的吗?
    猜你喜欢
    • 2012-05-29
    • 2014-01-02
    • 2017-01-25
    • 2013-01-18
    • 1970-01-01
    • 2013-10-05
    • 2015-04-11
    • 1970-01-01
    • 2013-11-26
    相关资源
    最近更新 更多