AFHTTPSessionManager - 获取反序列化/原始响应体(NSData?) [英] AFHTTPSessionManager - get unserialized/raw response body (NSData?)
问题描述
我根据推荐的iOS 8最佳实践(代替AFHTTPOperationManager,我之前使用过)将AFHTTPSessionManager子类化。
I've subclassed AFHTTPSessionManager according to the recommended best practice for iOS 8 (in place of AFHTTPOperationManager, which I was using before).
我可以抓住<来自任务
的code> NSHTTPURLResponse (除了没有正文,只有标题),回调返回序列化的 responseObject
这很好。
I can grab the NSHTTPURLResponse
from the task
(except that has no body, only headers), and the callback returns the serialized responseObject
which is fine.
有时我需要将响应记录为字符串或在文本字段中显示 - 似乎没有办法使用SessionManager本地执行此操作? OperationManager允许您将原始响应引用为NSString:
Sometimes I need to log the response as a string or display it in a text field - there doesn't appear to be a way to do this natively using SessionManager? OperationManager allowed you to reference the raw response as an NSString:
operation.responseString;
我想我可以对序列化的requestObject进行字符串化,但这似乎是很多不必要的开销,并赢了如果响应对象是无效的JSON,则会有所帮助。
I suppose I could stringify the serialized requestObject, but that seems like a lot of unnecessary overhead, and won't help if the response object is invalid JSON.
这是我的子类单例:
@implementation MyAFHTTPSessionManager
+ (instancetype)sharedManager {
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
然后做一个简单的GET(我已经添加到一个块中)方法),我可以这样做:
And then to make a simple GET (which I've added to a block method), I can do:
[[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);
}];
推荐答案
您可以通过创建自定义响应序列化程序来实现此目的使用标准响应序列化器记录数据并序列化响应,将原始数据和解析对象组合成自定义的复合响应对象。
You can accomplish this by creating a custom response serializer that records the data and serializes the response using the standard response serializer, combining both the raw data and parsed object into a custom, compound response object.
@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
然后在会话管理器上设置此序列化程序:
Then set this serializer on your session manager:
@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的实例:
Now in your completion handler you will get an instance of 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) {
}];
我刚刚编译而没有编译/测试,所以我会留给你调试和填写在差距中。
I just whipped this up without compiling/testing, so I will leave it to you to debug and fill in the gaps.
这篇关于AFHTTPSessionManager - 获取反序列化/原始响应体(NSData?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!