简体   繁体   English

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

[英]How to send URL Request using Rxalamofire in swift4

Actually i am new to iOS, and now my task is sending URL Request with Rxalamofire.I am Completely not aware of Rxalamofire. 实际上我是iOS新手,现在我的任务是发送带有Rxalamofire的URL请求。我完全不知道Rxalamofire。 Previously I was using only alamofire. 以前我只使用alamofire。 Previously I was using only alamofire.Now also I have sent url request like previous using previous, but later I found that Rxalamofire is much better than alamofire.Unfortunatly I am not able to send the URL request.So,Can any one tell me the step by step process.Thanks in advance. 以前我只使用alamofire.Now我也发送了url请求,就像之前使用的一样,但后来我发现Rxalamofire比alamofire好得多。不幸的是我无法发送URL请求。所以,任何人都可以告诉我一步一步的过程。谢谢。

 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)
                }

            }

I saw your earlier post struggling with MVVM/RxSwift. 我看到你早先的帖子在与MVVM / RxSwift挣扎。

You can find a project here that can help you with following- 你可以在这里找到一个项目,可以帮助你跟进 -

  1. RXSwift + MVVM RXSwift + MVVM
  2. Rxswift + Alamofire Rxswift + Alamofire

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

For the question, You can send the request the standard way, 对于这个问题,您可以通过标准方式发送请求,

But while using the RXswift API, it is better approach to use Observable pattern, which is observing streams of data. 但是在使用RXswift API时,使用Observable模式更好的方法是观察数据流。

Below code can help you get started. 下面的代码可以帮助您入门。

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();
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM