Ios 如何在swift4中使用Rxalamofire发送URL请求
实际上,我是iOS新手,现在我的任务是用Rxalamofire发送URL请求。我完全不知道Rxalamofire。以前我只使用阿拉莫菲尔。以前我只使用alamofire。现在我也像以前一样使用previous发送url请求,但后来我发现Rxalamofire比alamofire好得多。不幸的是,我无法发送url请求。因此,任何人都可以告诉我一步一步的过程。提前感谢Ios 如何在swift4中使用Rxalamofire发送URL请求,ios,swift4,Ios,Swift4,实际上,我是iOS新手,现在我的任务是用Rxalamofire发送URL请求。我完全不知道Rxalamofire。以前我只使用阿拉莫菲尔。以前我只使用alamofire。现在我也像以前一样使用previous发送url请求,但后来我发现Rxalamofire比alamofire好得多。不幸的是,我无法发送url请求。因此,任何人都可以告诉我一步一步的过程。提前感谢 postParameters = ["username":mailid,"password":password]
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)
}
}
我看到你之前的帖子在MVVM/RxSwift中苦苦挣扎 你可以在这里找到一个项目,它可以帮助你完成以下工作-
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();
})
}
func loginUser()->可观察到吗?{
返回可观察的。创建({observer in
/**
我们可以与url请求绑定的参数或附加头
这种情况在这里是标准的,就像在URLSession请求中一样
考虑这个例子来合并同样的例子。
https://stackoverflow.com/a/40608923/4549304
*/
让参数:[String:String]=[“用户名”:“此处为您的用户名值”,
“密码”:“此处为您的密码值”]
/**
有多种方法可以使用从服务器请求数据
默认ALamofire方法
我在这里使用Alamofire.request(String)方法实现同样的功能。
我们也可以使用
请求(urlRequest:URLRequestConvertible)
方法,以防我们要发出post请求
具有附加的标题和参数
*/
Alamofire.request(“https://reqres.in/api/login,方法:.post,参数:参数,编码:JSONEncoding.default,标题:nil)
.response{response in
如果响应.response?.statusCode==400{
打印(“授权错误”)
//您需要在此处退出请求,因为存在错误和错误
//响应不需要进一步解析
//您还可以使用observer.OnError(error)发送和发送错误
返回观察器.onCompleted()
}
//将数据转换为我们的模型并更新局部变量
guard let responseData=response.data else{
返回观察器.onCompleted()
}
做{
让model=try JSONDecoder().decode(YourModel.self,from:responseData)
observer.onNext(模型)
observer.onCompleted()
}抓住{
observer.onError(错误)
observer.onCompleted()
打印(“有些东西出错了。”)
}
}
返回一次性物品。创建();
})
}
我尝试了这一点。我在ViewModel类中实现了这一代码。但在这一行“let Model=try JSONDecoder().decode(YourModel.self,from:responseData)”中,我遇到了一个错误,如“参数类型中的'Model.type','Model'不符合预期的类型'Decodable'”,您希望在其中对数据建模的结构。。。一旦我分享了github链接示例,请仔细阅读。或者您可以对该行进行注释,然后以字符串格式打印响应。我采用了数组类型(array.self)。我在viewmodel类中编写了此函数。我在控制器类中的button action中调用它,如var loginscreen:viewmodel?登录屏幕!。loginUser()即使我单击按钮时没有调用它。我什么也没有得到。在上述情况下,loginScreen变量将为零,因为它是可选的且未初始化,要使其工作,您需要进行初始化,这可以通过执行loginscreen=viewModel()在viewDidLoad中完成。你也可以分享你的代码吗?一个github链接就可以了,我们可以在这里讨论问题。