AFHTTPSessionManager - 获取 unserialized/raw 响应 body(NSData?)

AFHTTPSessionManager - get unserialized/raw response body (NSData?)

我根据 iOS 8 的推荐最佳实践对 AFHTTPSessionManager 进行了子类化(代替我之前使用的 AFHTTPOperationManager)。

我可以从 task 中获取 NSHTTPURLResponse(除了没有 body,只有 headers),并且回调 returns 序列化responseObject 很好。

有时我需要将响应记录为字符串或将其显示在文本字段中 - 似乎没有办法使用 SessionManager 在本机执行此操作? OperationManager 允许您将原始响应引用为 NSString:

operation.responseString;

我想我可以将序列化的 requestObject 字符串化,但这似乎有很多不必要的开销,如果响应 object 无效 JSON.

也无济于事

这是我的子类单例:

@implementation MyAFHTTPSessionManager

+ (instancetype)sharedManager {
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

然后为了进行简单的 GET(我已将其添加到块方法),我可以这样做:

[[MyAFHTTPSessionManager sharedManager] GET:_url parameters:queryParams success:^(NSURLSessionDataTask *task, id responseObject) {
        completion(YES, task, responseObject, nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        completion(NO, task, nil, error);
    }];

您可以通过创建自定义响应序列化程序来实现此目的,该序列化程序记录数据并使用标准响应序列化程序序列化响应,将原始数据和解析的对象组合成自定义的复合响应对象。

@interface ResponseWithRawData : NSObject
@property (nonatomic, retain) NSData *data;        
@property (nonatomic, retain) id object;
@end

@interface ResponseSerializerWithRawData : NSObject <AFURLResponseSerialization>
- (instancetype)initWithForwardingSerializer:(id<AFURLResponseSerialization>)forwardingSerializer;
@end

...

@implementation ResponseWithRawData
@end

@interface ResponseSerializerWithRawData ()
@property (nonatomic, retain) forwardingSerializer;
@end

@implementation ResponseSerializerWithRawData

- (instancetype)initWithForwardingSerializer:(id<AFURLResponseSerialization>)forwardingSerializer {
    self = [super init];
    if (self) {
        self.forwardingSerializer = forwardingSerializer;
    }
    return self;
}

- (id)responseObjectForResponse:(NSURLResponse *)response
                       data:(NSData *)data
                      error:(NSError *__autoreleasing *)error {
    id object = [self.forwardingSerializer responseObjectForResponse:response data:data error:error];
    // TODO: could just log the data here and then return object; so that none of the request handlers have to change
    if (*error) {
        // TODO: Create a new NSError object and add the data to the "userInfo"
        // TODO: OR ignore the error and return the response object with the raw data only
        return nil;
    } else {
        ResponseWithRawData *response = [[ResponseWithRawData alloc] init];
        response.data = data;
        response.object = object;
        return response;
    }
}

@end

然后在您的会话管理器上设置此序列化程序:

@implementation MyAFHTTPSessionManager

+ (instancetype)sharedManager {
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
        instance.responseSerializer = [[ResponseSerializerWithRawData alloc] initWithForwardingSerializer:instance.responseSerializer];
    });
    return instance;
}

现在,在您的完成处理程序中,您将获得一个 ResponseWithRawData 实例:

[[MyAFHTTPSessionManager sharedManager] GET:_url parameters:queryParams success:^(NSURLSessionDataTask *task, id responseObject) {
    ResponseWithRawData *responseWithRawData = responseObject;
    NSLog(@"raw data: %@", responseWithRawData.data);
    // If UTF8 NSLog(@"raw data: %@", [[NSString alloc] initWithData:responseWithRawData.data encoding:NSUTF8StringEncoding]);
    // TODO: do something with parsed object
} failure:^(NSURLSessionDataTask *task, NSError *error) {

}];

我只是在没有 compiling/testing 的情况下完成了这个,所以我会把它留给你调试和填补空白。

您可以使用“AFNetworkingOperationFailingURLResponseDataErrorKey”键直接从 AFNetworking 访问“数据”对象,因此无需子类化 AFJSONResponseSerializer。您可以将数据序列化为可读的字典。这是一些获取 JSON 数据的示例代码:

NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
 NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: errorData options:kNilOptions error:nil];

这是在失败块中获取状态代码的代码

NSHTTPURLResponse* r = (NSHTTPURLResponse*)task.response;
NSLog( @"success: %d", r.statusCode );