获取Moya Response

时间:2017-01-03 10:10:19

标签: ios swift rx-swift moya

我正在使用RxSwift和Moya来呼叫请求并获得响应。

我的代码:

NetworkManager.shared.request(api: .carrot2diamond, showLoading: false).subscribe({ (response) in
// how to handle with response
}).addDisposableTo(self.disposeBag)

它显示如下:

  

[“Moya_Logger:[03/01/2017 16:52:50]要求:   http://api.360live.vn/api_shop/carrot2diamond?appversion=1.0&auth_key=f4aeaa8f9df1fd8ef68e1f3e431cd77995d565ef66e7dff9&devid=EA51920A-5C21-41D8-A420-62AF6AAD20FD&platform=2“]   [“Moya_Logger:[03/01/2017 16:52:50]请求标题:[:]”]   [“Moya_Logger:[03/01/2017 16:52:50] HTTP请求方法:GET”]   [“Moya_Logger:[03/01/2017 16:52:50]回复:{URL:   http://api.360live.vn/api_shop/carrot2diamond?appversion=1.0&auth_key=f4aeaa8f9df1fd8ef68e1f3e431cd77995d565ef66e7dff9&devid=EA51920A-5C21-41D8-A420-62AF6AAD20FD&platform=2   } {status code:200,headers {\ n \“Access-Control-Allow-Origin \”=   \“* \”; \ n \“Content-Length \”= 53; \ n \“Content-Type \”=   \“application / json; charset = utf-8 \”; \ n日期= \“星期二,2017年1月3日   09:52:50 GMT \“; \ n服务器= \”Jetty(9.2.z-SNAPSHOT)\“; \ n \ n
  \“X-Server \”= 360Live; \ n}}“] [”{\“error \”:0,\“message \”:\“exchange   胡萝卜值无效\“}”]

我想从这一行检测错误:

  

[“{\”error \“:0,\”message \“:\”交换胡萝卜值无效\“}”]

当我response.element?.response?.description时,它只是给我:

  

▿可选      - 某些:“{URL:http://api.360live.vn/api_shop/carrot2diamond?appversion=1.0&auth_key=f4aeaa8f9df1fd8ef68e1f3e431cd77995d565ef66e7dff9&devid=EA51920A-5C21-41D8-A420-62AF6AAD20FD&platform=2   } {status code:200,headers {\ n \“Access-Control-Allow-Origin \”=   \“* \”; \ n \“Content-Length \”= 53; \ n \“Content-Type \”=   \“application / json; charset = utf-8 \”; \ n日期= \“星期二,2017年1月3日   09:52:50 GMT \“; \ n服务器= \”Jetty(9.2.z-SNAPSHOT)\“; \ n \ n
  \“X-Server \”= 360Live; \ n}}“

1 个答案:

答案 0 :(得分:0)

我刚刚在调用mapJSON()后添加request来解决此问题。

mapJSON()的声明是

func mapJSON(failsOnEmptyData: Bool = default) -> Observable<Any>

它的描述说:

Maps data received from the signal into a JSON object. If the conversion fails, the signal errors.

我的代码:

NetworkManager.shared.request(api: .carrot2diamond, showLoading: false).mapJSON().subscribe({ (response) in
   if let element = response.element, let dic = element as? [String: AnyObject], let message = dic["message"] as? String {
       print(message) // ->>>> exchange carrot value invalid
   }
}).addDisposableTo(self.disposeBag)