2017-01-03 109 views
0

我正在使用RxSwift和Moya來調用請求並獲取響應。從Moya Response獲取響應

我的代碼:

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

它顯示了這樣的事情:

[ 「Moya_Logger:[2017年3月1日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 Date = \」2017年1月3日星期二 09:52:50 GMT \「; \ n Server = \」Jetty(9.2.z-SNAPSHOT)\「; \ n
\ 「X-服務器\」= 360Live; \ N}} 「] [」{\ 「錯誤\」:0,\ 「消息\」:\ 「交換 胡蘿蔔值無效\」}「]

我想從該行檢測錯誤:

[ 「{\」 錯誤\ 「:0,\」 消息\ 「:\」 交換胡蘿蔔值無效\ 「}」]

當我po response.element?.response?.description,它只是給我:

▿可選 - 一些: 「{網址:http://api.360live.vn/api_shop/carrot2diamond?appversion=1.0&auth_key=f4aeaa8f9df1fd8ef68e1f3e431cd77995d565ef66e7dff9&devid=EA51920A-5C21-41D8-A420-62AF6AAD20FD&platform=2 } {狀態代碼:200,頭{\ n \」 訪問控制允許來源\」 = \ 「* \」; \ n \「Content-Length \」= 53; \ n \「Content-Type \」= \「application/json; charset = utf-8 \「; \ n Date = \」2017年1月3日星期二 09:52:50 GMT \「; \ n Server = \」Jetty(9.2.z-SNAPSHOT)\「; \ n
\ 「X-服務器\」= 360Live; \ n}}」

+0

的'response'你說的是'事件' – Khuong

回答

0

我剛纔通過調用request後加入mapJSON()解決了這個問題。

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. 

我的代碼:

​​