responseJSON没有响应

时间:2016-06-21 19:08:31

标签: json swift alamofire imageshack

我正在使用Alamofire尝试使用image shack API在图像小屋服务器上放置图像。我没有得到回复并得到错误:

  

FAILURE:Error Domain = NSCocoaErrorDomain Code = 3840“无效的值   在角色0周围。

我也强调了错误在代码中的引用位置:

        let imageName = "pano.jpg"
        let postImage = UIImage(named: imageName)
        let urlStr = "https://post.imageshack.us/upload_api.php"
        let theUrl:NSString = urlStr.stringByRemovingPercentEncoding!
        let url = NSURL(string: theUrl as String)!
        let imgData = UIImageJPEGRepresentation(postImage!, 0.2)!
        let keyData = "0345CHKMad115dd32980b363be5f2d34731b8110".dataUsingEncoding(NSUTF8StringEncoding)!
        let keyJSON = "json".dataUsingEncoding(NSUTF8StringEncoding)!


Alamofire.upload(.POST, url, multipartFormData: { MultipartFormData in

                MultipartFormData.appendBodyPart(data: imgData, name: "fileupload", mimeType: "image/jpg")
                MultipartFormData.appendBodyPart(data: keyData, name: "key")
                MultipartFormData.appendBodyPart(data: keyJSON, name: "format")

            }) {encodingResult in

                switch encodingResult {
                case .Success(let upload, _, _):
                    upload.responseJSON(completionHandler: { response in

                        print(response) //ERROR HERE
                        guard let data = response.result.value
                            else{
                                print("Request failed with error data)")

                            return
                        }

                        if let info = data as? Dictionary<String, AnyObject> {
                            if let links = info["links"] as? Dictionary<String, AnyObject> {
                                if let imgLink = links["image_link"] as? String {
                                    print(imgLink)
                                }
                            }
                        }
                    })

我完全迷失了这个问题意味着任何帮助都会很棒。

2 个答案:

答案 0 :(得分:0)

在将url传递给函数do:

之前,请尝试此操作
//Convert the NSURL to a NSString
let urlString:NSString = url.absoluteString() as! NSString

//Remove Percent Encoding
let theUrl:NSString = urlString.stringByRemovingPercentEncoding()

//Place Back into NSURL
let newUrl = NSURL(string:theUrl)

并通过newUrl代替url。最好在实例化原始url之前执行此操作,但请尝试并查看是否有帮助。

修改

在遇到问题的地方尝试这个:

var jsonError:NSError?
let json = NSJSONSerialization.JSONObjectWithData(response, options: nil, error: &jsonError) as NSDictionary {

    print(json)
}

答案 1 :(得分:0)

   switch encodingResult {
                    case .Success(let upload, _,_):
                        upload.responseJSON(completionHandler: {response in

                            if let data = response.result.value as? Dictionary<String, AnyObject>{

                                if let links = data["links"] as? Dictionary<String, AnyObject> {
                                    if let imgLink = links["image_link"] as? String {
                                        print(imgLink)
                                    }
                                }
                            }


                        })
                    case .Failure(let error):
                        print(error)
                    }