【问题标题】:Alamofire .responseJSON doesn't work with JSON responseAlamofire .responseJSON 不适用于 JSON 响应
【发布时间】:2016-08-19 18:17:56
【问题描述】:

我正在使用 alamofire 上传图片。 这是注册 API,您可以在其中使用您的个人资料照片进行注册(这是我必须上传的)

所以我的代码是这样的(我将用另一个代码替换 print(JSON);这只是为了测试有什么问题)

func makeUploadRequest(url: String?) {

    let imageData = NSData(data: UIImageJPEGRepresentation(self.userImage.image!, 1)!)

    Alamofire.upload(.POST, url!, headers: ["Content-Type":"application/json"], multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(data: imageData, name: "image_file_1")
        }, encodingCompletion: {
            encodingResult in

            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON { (JSON) in
                    print(JSON)
                }

            case .Failure(let encodingError):
                //Show Alert in UI
                print(encodingError)
            }
    })
}

但是当我运行这段代码时,我遇到了这条消息:

FAILURE: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around
character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}

我知道为什么会收到此消息,因为响应不是 JSON 格式。 但响应实际上是 JSON

{
    result: "success",
    msg: "",
    data: {...}
}

当我使用 URL 测试 API 时,它工作得很好。

当我使用 .responseString 而不是 .responseJSON 时:它说明了 ASP.NET

.响应:

(Optional(<NSMutableURLRequest: 0x7f8353217d50> { URL: url }),
 Optional(<NSHTTPURLResponse: 0x7f8353099040> { URL: url }
{ status code: 500, headers {
"Cache-Control" = private;
"Content-Length" = 5136;
"Content-Type" = "text/html; charset=utf-8";
Date = "Tue, 26 Apr 2016 06:09:03 GMT";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "2.0.50727";
"X-Powered-By" = "ASP.NET";
} }), Optional(<3c68746d ... 2e2d2d3e>), nil)

有什么帮助吗?提前致谢!

【问题讨论】:

  • 试试.responseString而不是.responseJSON,你就会知道你在发送请求时犯了什么错误。
  • @Fennec 哦,我试过 .responseString 和 .response。我将用这些编辑我的问题

标签: ios json swift multipartform-data alamofire


【解决方案1】:

尝试替换成功块:-

case .Success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                    AppHelper.hideLoadingSpinner()
                    if response.result.value is NSNull
                    {
                        print("Response nil")
                    }
                    else
                    {
                        if let JSON = response.result.value {
                            var mydict = NSMutableDictionary()
                            mydict = JSON as! NSMutableDictionary

                            if (self.delegate != nil){
                                print("response:--------------------\n %@",mydict)
                            }
                        }
                        else
                        {
                            print("response not converted to JSON")
                        }

                    }
                    upload.response(completionHandler: { (request, response, data, error) -> Void in

                            NSLog("upload.response : data : %@", String(data: data!, encoding: NSUTF8StringEncoding)!)
                            NSLog("upload.response : response : %@", response!)


                        })
    }

【讨论】:

  • 试过这个;响应未转换为 JSON!
  • 好的,现在你想看看回复我编辑我的问题只是参考。
  • upload.response : response : { URL: ~~url~~ } { status code: 500, headers { "Cache-Control" = private; “内容长度”= 5136; “内容类型”=“文本/html;字符集=utf-8”;日期 =“2016 年 4 月 26 日星期二 05:59:39 GMT”;服务器 = "微软-IIS/7.5"; "X-AspNet-版本" = "2.0.50727"; "X-Powered-By" = "ASP.NET"; } }
  • 这就是我所拥有的; HTTP 500 服务器错误?唔。当我已经将它设置为“application/json”时,为什么内容类型是“text/html”?
  • 现在你得到了什么实际问题,所以你用 api 开发者解决。
【解决方案2】:

在获取值之前检查您的结果是失败还是成功。例如:

switch response.result {
case .Success(let value):
    print("Value: \(value)")
    break
case .Failure(let error):
    print("Error: \(error)")
    break
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多