我在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;