ios – Alamofire POST请求有进展
发布时间:2020-12-14 19:46:21 所属栏目:百科 来源:网络整理
导读:我正在使用Alamofire进行POST请求. 由于此POST请求可能需要一段时间,我想跟踪进度并将其显示为ProgressView. Alamofire.request(.POST,ApiLink.create_post,parameters: parameters,encoding: .JSON) .progress { (bytesRead,totalBytesRead,totalBytesExpec
我正在使用Alamofire进行POST请求.
由于此POST请求可能需要一段时间,我想跟踪进度并将其显示为ProgressView. Alamofire.request(.POST,ApiLink.create_post,parameters: parameters,encoding: .JSON) .progress { (bytesRead,totalBytesRead,totalBytesExpectedToRead) -> Void in println("ENTER .PROGRESSS") println("(totalBytesRead) of (totalBytesExpectedToRead)") self.progressView.setProgress(Float(totalBytesRead) / Float(totalBytesExpectedToRead),animated: true) } .responseJSON { (_,_,mydata,_) in println(mydata) } 但是,我注意到.progress块只在post请求结束后才被调用,而不是多次调用以实际跟踪进度. 我如何使.progress与Alamofire.request POST一起使用? 另外:我的参数包括base64编码的图像字符串.我正在使用后端Ruby on Rails来处理图像.这个过程需要相当长的时间. 解决方法
你可以做的是首先使用ParameterEncoding枚举来生成HTTPBody数据.然后,您可以将数据拉出并将其传递给Alamofire上传方法.这是同一个功能的修改版本,它在游乐场中编译,而不是使用上传功能.
struct ApiLink { static let create_post = "/my/path/for/create/post" } let parameters: [String: AnyObject] = ["key": "value"] // Make sure this has your image as well let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: ApiLink.create_post)!) mutableURLRequest.HTTPMethod = Method.POST.rawValue let encodedURLRequest = ParameterEncoding.JSON.encode(mutableURLRequest,parameters: parameters).0 let data = encodedURLRequest.HTTPBody! let progressView = UIProgressView() Alamofire.upload(mutableURLRequest,data) .progress { _,totalBytesExpectedToRead in println("ENTER .PROGRESSS") println("(totalBytesRead) of (totalBytesExpectedToRead)") progressView.setProgress(Float(totalBytesRead) / Float(totalBytesExpectedToRead),animated: true) } .responseJSON { _,_ in println(mydata) } 正如@mattt最初在上面的评论中提到的,这肯定会有进展更新. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |