这是我的代码..它是一个非常简单的操作
[self GET:operationName parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"%@", responseObject);
//do something upon success
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
//do something to handle error
}];
我的问题是,我需要看看确切的原始json响应是什么..当我NSLog的responseObject时,它与我从独立的HTTP客户端得到的JSON输出不一样_我猜它是因为它已经通过了串行器?
答案 0 :(得分:4)
如果您不希望它NSJSONSerialization
转换为NSArray
/ NSDictionary
,而是想要原始NSData
,则应设置{ responseSerializer
到AFURLSessionManager
的{1}}。
AFHTTPResponseSerializer
默认值为self.responseSerializer = [AFHTTPResponseSerializer serializer];
。如果您不希望它为您转换JSON,请更改响应序列化程序。
答案 1 :(得分:4)
您可以使用“AFNetworkingOperationFailingURLResponseDataErrorKey”键直接从AFNetworking访问“data”对象,因此无需对AFJSONResponseSerializer进行子类化。您可以将数据序列化为可读字典。 以下是一些示例代码:
NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: errorData options:kNilOptions error:nil];
答案 2 :(得分:1)
很难使用他们的API挂钩AF的原始响应,即使在调试级别,他们的logger也会在输出到控制台之前反序列化JSON。
我在AFURLSessionManager.m文件中添加了以下行。
NSLog(@"Response String: %@", [[NSString alloc] initWithData:[NSData dataWithData:self.mutableData] encoding:NSUTF8StringEncoding]);
在此NSURLSessionTaskDelegate委托方法
中- (void)URLSession:(__unused NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
这里是在上下文中(对于成功的响应,没有错误)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
__strong AFURLSessionManager *manager = self.manager;
__block id responseObject = nil;
__block NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
userInfo[AFNetworkingTaskDidCompleteResponseSerializerKey] = manager.responseSerializer;
if (self.downloadFileURL) {
userInfo[AFNetworkingTaskDidCompleteAssetPathKey] = self.downloadFileURL;
} else if (self.mutableData) {
userInfo[AFNetworkingTaskDidCompleteResponseDataKey] = [NSData dataWithData:self.mutableData];
}
if (error) {
userInfo[AFNetworkingTaskDidCompleteErrorKey] = error;
dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
if (self.completionHandler) {
self.completionHandler(task.response, responseObject, error);
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
});
});
} else {
dispatch_async(url_session_manager_processing_queue(), ^{
NSError *serializationError = nil;
NSLog(@"Response String: %@", [[NSString alloc] initWithData:[NSData dataWithData:self.mutableData] encoding:NSUTF8StringEncoding]);
responseObject = [manager.responseSerializer responseObjectForResponse:task.response data:[NSData dataWithData:self.mutableData] error:&serializationError];
if (self.downloadFileURL) {
responseObject = self.downloadFileURL;
}
if (responseObject) {
userInfo[AFNetworkingTaskDidCompleteSerializedResponseKey] = responseObject;
}
if (serializationError) {
userInfo[AFNetworkingTaskDidCompleteErrorKey] = serializationError;
}
dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
if (self.completionHandler) {
self.completionHandler(task.response, responseObject, serializationError);
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
});
});
});
}
#pragma clang diagnostic pop
}