美文网首页iOS技能
AFN3.0 如何实现operation.responseStr

AFN3.0 如何实现operation.responseStr

作者: 李小争 | 来源:发表于2016-06-01 16:51 被阅读966次

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

    发现还有好多朋友还不清楚如何实现,其实很简单

    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更-----
    作者回复中已经添加了相关操作
    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;
    

    相关文章

      网友评论

      • MrStock:大神您好!我看了afnet的解释,但是不知到怎么在successblock里面得到这个responseString。其实不是我找事,是导入别家的sdk,他们demo用的旧的2x版。我们用的新版,他们用到了这个属性。谢谢解答!
      • ylgwhyh:如果我要得到正常放回的json呢?不是报错是的
      • 纯阳子_:你确定是获取到的Body而不是header??

      本文标题:AFN3.0 如何实现operation.responseStr

      本文链接:https://www.haomeiwen.com/subject/bumhdttx.html