我正在尝试将一张图片通过JSON文件发送到Android应用程序,以下是我的代码:
但是当我的合作伙伴尝试获取响应时,他会收到此消息:
当我在注释中添加“'pic' => $pic = ($car->getPic())”时,他会接收到所有没有图片的汽车。
如何在JSON文件中发送图像?
PS:我已经花了3天时间尝试解决这个问题,但没取得任何进展。我尝试使用http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html,但不起作用。
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,但不起作用。
Symfony2 -> Android
还是反过来? - Jovan Perovic$car->getPic()
是否返回预期数据?尝试在浏览器中调用此操作,并将$pic
的内容进行 echo 输出。 - Jovan Perovic