AFN3.0 如何實現(xiàn)operation.responseString

我在AFNetworking issues中的回答how to get response body in failure block?還有
How can i get responseString which at AFNetworking 3.0

發(fā)現(xiàn)還有好多朋友還不清楚如何實現(xiàn),其實很簡單

NSError *underError = error.userInfo[@"NSUnderlyingError"];
NSData *responseData = underError.userInfo[@"com.alamofire.serialization.response.error.data"];

有小伙伴問我com.alamofire.serialization.response.error.data是怎么來的,原來都是AFJSONResponseSerializer解析修改成manager.responseSerializer = [AFHTTPResponseSerializer serializer];這樣報錯的時候就會看到控制臺打印的信息里面有com.alamofire.serialization.response.error.data后面輸出responseData...然后才有了上面的解決辦法

-----16.6.7更-----
作者回復(fù)中已經(jīng)添加了相關(guān)操作
AFURLResponseSerialization
可以在 failure block 中添加下面代碼獲取responseString

NSData *data = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];

具體代碼:

/**
 ## User info dictionary keys

 These keys may exist in the user info dictionary, in addition to those defined for NSError.

 - `NSString * const AFNetworkingOperationFailingURLResponseErrorKey`
 - `NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey`

 ### Constants

 `AFNetworkingOperationFailingURLResponseErrorKey`
 The corresponding value is an `NSURLResponse` containing the response of the operation associated with an error. This key is only present in the `AFURLResponseSerializationErrorDomain`.

 `AFNetworkingOperationFailingURLResponseDataErrorKey`
 The corresponding value is an `NSData` containing the original data of the operation associated with an error. This key is only present in the `AFURLResponseSerializationErrorDomain`.
 */
FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseErrorKey;

FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容