2

我正在使用 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=EA51920AFD40-&6=6AD28 "] ["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=EA1DAD820A&-5C221-64 =2 } { 状态码:200,标头 {\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
\"X-Server\" = 360Live;\n} }"] ["{\"error\":0,\"message\":\"交换胡萝卜值无效\"}"]

我想从这一行检测错误:

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

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

▿ 可选 - some : " { URL: http://api.360live.vn/api_shop/carrot2diamond?appversion=1.0&auth_key=f4aeaa8f9df1fd8ef68e1f3e431cd77995d565ef66e7dff9&devid=EA51920A-5C21-41D8-A420-62AF6AAD20FD&平台 {} \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
\"X -服务器\" = 360Live;\n} }"

4

1 回答 1

2

我刚刚通过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)
于 2017-01-03T10:33:30.177 回答