首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在swift4中使用Rxalamofire发送URL请求

如何在swift4中使用Rxalamofire发送URL请求
EN

Stack Overflow用户
提问于 2019-04-03 13:18:54
回答 1查看 1.8K关注 0票数 1

实际上,我是iOS的新手,现在我的任务是用Rxalamofire发送URL请求,我完全不知道Rxalamofire。以前我只用的是阿莫芬火。以前我只使用alamofire.Now,我也像以前一样发送了url请求,但是后来我发现Rxalamofire比alamofire.Unfortunatly要好得多,我无法发送URL request.So,谁能提前告诉我逐步的process.Thanks。

代码语言:javascript
复制
 postParameters = ["username":mailid,"password":password]

            Alamofire.request(Constants.loginapi, method: .post, parameters: postParameters, encoding: URLEncoding.default, headers: nil).responseJSON {  response in
                switch response.result {
                case .success:
                    print(response)
                  case .failure(let error):
                    print(error)
                }

            }
EN

回答 1

Stack Overflow用户

发布于 2019-04-04 07:57:14

我看到您之前的文章与MVVM/RxSwift进行了斗争。

你可以在这里找到一个能帮你做以下事情的项目-

  1. RXSwift + MVVM
  2. 雷克斯斯威夫特+阿拉莫火

https://github.com/saurabh-360/RxAlamofireDemo

对于这个问题,你可以用标准的方式发送请求,

但是,在使用RXswift API时,更好的方法是使用可观察的模式,即观察数据流。

下面的代码可以帮助您入门。

代码语言:javascript
复制
func loginUser() -> Observable<Any>? {

    return Observable<Any>.create({observer in
        /**
         parameters or additional headers we can bind with the url request
         the case is standard here like we do in URLSession requests
         consider this example to incorporate the same
         https://stackoverflow.com/a/40608923/4549304
         */
        let parameters:[String:String] = ["username":"Your username value here",
                                          "password":"Your password value here"]

        /**
         There are multiple ways to request data from the servers using
         Default ALamofire methods
         I am using Alamofire.request(String) method here for the same.


         We Can also use
         request(_ urlRequest: URLRequestConvertible)
         method in case we want to make a post request
         with additional headers and parameters

         */

        Alamofire.request("https://reqres.in/api/login", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil)
            .response { response in
                if response.response?.statusCode == 400 {
                    print("authorisation error")

                    // you need to exit the request here since there was error and
                    // no parsing is needed further for the response
                    // you can also send and error using the observer.OnError(Error)
                    return observer.onCompleted()
                }

                // convert data to our model and update the local variable
                guard let responseData =  response.data else {
                    return observer.onCompleted()
                }
                do {
                    let model = try JSONDecoder().decode(YourModel.self, from: responseData)
                    observer.onNext(model)
                    observer.onCompleted()
                }catch {
                    observer.onError(error)
                    observer.onCompleted()
                    print("some thing went wrong.")
                }
        }

        return Disposables.create();
    })
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55496192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档