Alamofire:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

时间:2018-07-26 09:29:32

标签: ios swift alamofire

我正在尝试通过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传递了十多个参数。

2 个答案:

答案 0 :(得分:3)

只需从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)
            }

        })

答案 1 :(得分: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)
    }
    )