gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:56:56 25 4
gpt4 key购买 nike

我正在尝试将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com