使用PHP下载JSON格式的图像数据(blob)
我试图使用Swift发送web请求,以获取JSON格式的图像数据,后端的代码使用PHP,因此我的PHP代码如下所示使用PHP下载JSON格式的图像数据(blob),php,ios,mysql,json,swift,Php,Ios,Mysql,Json,Swift,我试图使用Swift发送web请求,以获取JSON格式的图像数据,后端的代码使用PHP,因此我的PHP代码如下所示 function readImage($productID, $conn){ $query = "SELECT * FROM ProductImage WHERE ProductID='" . $productID . "'"; $result = $conn->query($query); $i = 0; while($row = $resu
function readImage($productID, $conn){
$query = "SELECT * FROM ProductImage WHERE ProductID='" . $productID . "'";
$result = $conn->query($query);
$i = 0;
while($row = $result->fetch_assoc())
{
$imageDatas = $row["ImageData"];
$imageDatas = base64_encode($imageDatas);
$i = $i + 1;
}
if($i == 0){
return array("Error" => true);
}
else{
$response = array("Error" => false);
$response["ImageDatas"] = $imageDatas;
$result = array("Response" => $response);
return json_encode($result);
}
}
{
"Response": {
"Error": false,
"ImageDatas": "very long string (image data)"
}
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
var error: NSError?
self.jsonResponse = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as! Dictionary <String, AnyObject>
}
我正在使用PostMan测试我的API,当我发送请求以使用PostMan检索图像时,它工作正常,结果如下
function readImage($productID, $conn){
$query = "SELECT * FROM ProductImage WHERE ProductID='" . $productID . "'";
$result = $conn->query($query);
$i = 0;
while($row = $result->fetch_assoc())
{
$imageDatas = $row["ImageData"];
$imageDatas = base64_encode($imageDatas);
$i = $i + 1;
}
if($i == 0){
return array("Error" => true);
}
else{
$response = array("Error" => false);
$response["ImageDatas"] = $imageDatas;
$result = array("Response" => $response);
return json_encode($result);
}
}
{
"Response": {
"Error": false,
"ImageDatas": "very long string (image data)"
}
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
var error: NSError?
self.jsonResponse = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as! Dictionary <String, AnyObject>
}
但是,在我的Swift代码中,当我获得请求响应并尝试将数据转换为JSON格式时,我会得到以下错误:
(NSError?) error = domain: "NSCocoaErrorDomain" - code: 3840 {
ObjectiveC.NSObject = {}
}
我用谷歌搜索了这个错误,人们说返回的数据不是正确的JSON格式,我的Swift代码看起来像这样
function readImage($productID, $conn){
$query = "SELECT * FROM ProductImage WHERE ProductID='" . $productID . "'";
$result = $conn->query($query);
$i = 0;
while($row = $result->fetch_assoc())
{
$imageDatas = $row["ImageData"];
$imageDatas = base64_encode($imageDatas);
$i = $i + 1;
}
if($i == 0){
return array("Error" => true);
}
else{
$response = array("Error" => false);
$response["ImageDatas"] = $imageDatas;
$result = array("Response" => $response);
return json_encode($result);
}
}
{
"Response": {
"Error": false,
"ImageDatas": "very long string (image data)"
}
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
var error: NSError?
self.jsonResponse = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as! Dictionary <String, AnyObject>
}
func连接(连接:NSURLConnection,didReceiveData:NSData){
var错误:N错误?
self.jsonResponse=NSJSONSerialization.JSONObjectWithData(数据,选项:nil,错误:&error)as!Dictionary
}
知道发生了什么吗?任何小小的帮助都将不胜感激,因为我已经没有选择了
谢谢,我发现了我的错误。
我在“didReceiveData”函数中处理响应数据,所以这有点早,因为数据没有完全下载;因此,当我试图将数据序列化为JSON时,出现了上述错误。但是,当我接收到小数据(如文本)时,上述代码工作正常
因此,我必须在“ConnectiondFinishLoading”函数中处理接收到的数据,因此我的最终代码如下所示
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
self.receivedData = NSMutableData()
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
self.receivedData .appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection) {
var error: NSError?
if let dict = NSJSONSerialization.JSONObjectWithData(self.receivedData, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary {
self.jsonResponse = dict as! Dictionary<String, AnyObject>
NSNotificationCenter.defaultCenter().postNotificationName("ResponseWithSuccess", object: self.jsonResponse)
} else {
// unable to paress the data to json, handle error.
}
}
func连接(连接:NSURLConnection,didReceiverResponse响应:nsurResponse){
self.receivedData=NSMutableData()
}
func连接(连接:NSURLConnection,didReceiveData:NSData){
self.receivedData.appendData(数据)
}
func ConnectionIDFinishLoading(连接:NSURLConnection){
var错误:N错误?
如果让dict=NSJSONSerialization.JSONObjectWithData(self.receivedData,选项:NSJSONReadingOptions.MutableContainers,错误:&error)作为NSDictionary{
self.jsonResponse=dict as!Dictionary
NSNotificationCenter.defaultCenter().postNotificationName(“ResponseWithSuccess”,对象:self.jsonResponse)
}否则{
//无法将数据还原为json,请处理错误。
}
}
我发现了我的错误。
我在“didReceiveData”函数中处理响应数据,所以这有点早,因为数据没有完全下载;因此,当我试图将数据序列化为JSON时,出现了上述错误。但是,当我接收到小数据(如文本)时,上述代码工作正常
因此,我必须在“ConnectiondFinishLoading”函数中处理接收到的数据,因此我的最终代码如下所示
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
self.receivedData = NSMutableData()
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
self.receivedData .appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection) {
var error: NSError?
if let dict = NSJSONSerialization.JSONObjectWithData(self.receivedData, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary {
self.jsonResponse = dict as! Dictionary<String, AnyObject>
NSNotificationCenter.defaultCenter().postNotificationName("ResponseWithSuccess", object: self.jsonResponse)
} else {
// unable to paress the data to json, handle error.
}
}
func连接(连接:NSURLConnection,didReceiverResponse响应:nsurResponse){
self.receivedData=NSMutableData()
}
func连接(连接:NSURLConnection,didReceiveData:NSData){
self.receivedData.appendData(数据)
}
func ConnectionIDFinishLoading(连接:NSURLConnection){
var错误:N错误?
如果让dict=NSJSONSerialization.JSONObjectWithData(self.receivedData,选项:NSJSONReadingOptions.MutableContainers,错误:&error)作为NSDictionary{
self.jsonResponse=dict as!Dictionary
NSNotificationCenter.defaultCenter().postNotificationName(“ResponseWithSuccess”,对象:self.jsonResponse)
}否则{
//无法将数据还原为json,请处理错误。
}
}