Alamofire:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputData

Posted

技术标签:

【中文标题】Alamofire:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)【英文标题】:Alamofire: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) 【发布时间】:2018-07-26 09:29:31 【问题描述】:

我正在尝试通过 alamofire 上传数据,包括图片。但它会返回

responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

-- 我的代码是,

    Alamofire.upload(multipartFormData:  (multipartFormData) in

            multipartFormData.append((userid?.data(using: String.Encoding.utf8))!, withName: "user_id")

            multipartFormData.append((self.addUserBtnTag?.description.data(using: String.Encoding.utf8))!, withName: "slot")
, to: urlString, method: .post, encodingCompletion:  (result) in
 switch result 
            case .success(let upload, _, _):

                upload.responseJSON  response in

                    print(response.result)
                    print("Response : ", response)

                    if response.result.isSuccess
                    
                        let JSON = response.result.value as? NSDictionary

                        print("EditJSON : ", JSON!)
                    
                
            case .failure(let encodingError):
                print(encodingError)
            
        )

但实际上我通过 alamofire 传递了十多个参数。

【问题讨论】:

您的回复为空或无。尝试在邮递员中执行此操作。 它在邮递员中给出响应。**responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)** bold 这是作为错误返回而不是作为响应。如果值nil 那么它必须将响应作为缺少的参数。 你也可以添加你的邮递员回复吗? 不要让 Postman 误导您。它发生在我身上一次。邮递员“非常好”,即使在***范围大括号之外存在不相关的文本,它也能够解析 json。例如。 blahblah "someProperty":"someValue" 可以在 Postman 上工作,但不能在 Alamofire 上工作。因此,请检查您的回复的原始文本,看看您是否获得了可解析的数据。 谢谢@SharadChauhan 和 ** AuRis** 从你的回答中可以清楚地看到 :) 【参考方案1】:

只需将responseJSON 更改为responseString

Alamofire.upload(multipartFormData:  (multipartFormData) in

            multipartFormData.append((userid?.data(using: String.Encoding.utf8))!, withName: "user_id")

            multipartFormData.append((self.addUserBtnTag?.description.data(using: String.Encoding.utf8))!, withName: "slot")
, to: urlString, method: .post, encodingCompletion:  (result) in
 switch result 
            case .success(let upload, _, _):

                upload.responseString  response in

                    print(response.result)
                    print("Response : ", response)

                    if response.result.isSuccess
                    
                        let JSON = response.result.value as? NSDictionary

                        print("EditJSON : ", JSON!)
                    
                
            case .failure(let encodingError):
                print(encodingError)
            

        )

【讨论】:

【参考方案2】:

尝试使用 alamofire 上传带有参数的图像

 Alamofire.upload(multipartFormData:  (multipartFormData) in

        let imageData: NSData? = UIImageJPEGRepresentation(image, 0.5) as NSData?
        if imageData != nil 

            multipartFormData.append(imageData! as Data, withName: strImageKey, fileName: "imagep.jpeg", mimeType: "imagep/jpeg")

            for (key, value) in dictParams 
                let data = "\(value)".data(using: .utf8)
                multipartFormData.append(data! as Data, withName: key)
            
        

    , with: requestName,encodingCompletion:  encodingResult in
        switch encodingResult 
        case .success(let upload, _, _):

            upload.responseJSON  response in
                log.debug("Image(s) Uploaded successfully:\(response)")
                //Networking.handleApiResponse(response)
            
        case .failure(let encodingError):
            log.debug("encodingError:\(encodingError)")
            // Networking.handleApiResponse(response)

            Util.showAlertWithMessage(msgSorry, title:"Error")
        
        completionHandler!(encodingResult)
    
    )

【讨论】:

以上是关于Alamofire:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputData的主要内容,如果未能解决你的问题,请参考以下文章

导入 Alamofire 3.4 - 没有这样的模块“Alamofire”

Alamofire:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputData

出现错误:使用 Alamofire.playground 时没有这样的模块“Alamofire”

var 请求:Alamofire.Request?使用未声明类型的 Alamofire

值:(failure(Alamofire.AFError.explicitlyCancelled)) 使用 Alamofire 发布者时

使用方案 Alamofire watchOS 构建工作区 Alamofire 失败