gpt4 book ai didi

json - responseJSON 没有得到响应

转载 作者:行者123 更新时间:2023-11-30 13:18:56 26 4
gpt4 key购买 nike

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

FAILURE: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 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)
}
}
}
})

我完全不知道这个问题意味着什么,任何帮助都会很棒。

最佳答案

在将 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)
}

关于json - responseJSON 没有得到响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37952680/

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