1

I am trying to send web request using Swift to get image data in JSON format, the code in the backend using PHP, so my PHP code looks like

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);
    }
}

And I am using PostMan to test my API and when I send request to retrieve the image using PostMan, it works fine and the result looks like

{
  "Response": {
    "Error": false,
    "ImageDatas": "very long string (image data)"
  }
}

However, in my Swift code, when I get the request response and try to convert the data to JSON format I get the following error:

(NSError?) error = domain: "NSCocoaErrorDomain" - code: 3840 {
  ObjectiveC.NSObject = {}
}

I have googled this error and people say the returned data is not in proper JSON format, where my Swift code looks like

func connection(connection: NSURLConnection, didReceiveData data: NSData) {
        var error: NSError?
        self.jsonResponse = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as! Dictionary <String, AnyObject>

    }

Any idea what's going on? Any little help would be appreciated, as I run out of options.

Thanks

Ofcourse
  • 617
  • 1
  • 7
  • 19
  • possible duplicate of [how to upload images to a server in iOS swift](http://stackoverflow.com/questions/26335656/how-to-upload-images-to-a-server-in-ios-swift) – srinivas n Jul 30 '15 at 12:00

1 Answers1

0

I have found my mistake. I was handling the response data in "didReceiveData" function, so that was a bit early as the data wasn't completely downloaded; thus, when I was trying to serialise the data to JSON, I had the above error. However the above code works fine when I receive small data, such text.

Therefore, I had to handle the received data in "connectionDidFinishLoading" function, so my final code looks like.

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.
        }


    }
Ofcourse
  • 617
  • 1
  • 7
  • 19