2015-05-28 24 views
3

我想要在JSON文件發送圖像到這裏Android應用是我的代碼:在JSON文件發送BLOB數據與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 =($小車 - >方法GetPic()));」在評論中,他收到沒有圖像的所有車輛。

我該如何發送json文件中的圖像?

PS我花了3天試圖解決這個問題,但沒有得到任何地方。我試圖使用這個http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html但沒有工作。

回答

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的功能。

希望這會有所幫助...

+0

我得到它編碼,我保存它,因爲它是。這是我保存圖像錯誤的方式嗎? – daroum

+0

只是爲了澄清:數據是'Symfony2 - > Android'還是反之呢? –

+0

Symfony2 - > Android – daroum