无法使用 AFNetworking 3 为 JSON 和 XML 响应创建单例
Cannot make singleton for JSON and XML response using AFNetworking 3
您好,我正在为 JSON 和 XML 响应制作一个网络单例
我的ApiClient.h
+ (ApiClient *)sharedInstance;
-(instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration;
我的ApiClient.m
+ (ApiClient *)sharedInstance {
static ApiClient *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:SINGPOST_BASE_URL] sessionConfiguration:sessionConfiguration];
[sharedInstance setDataTaskWillCacheResponseBlock:^NSCachedURLResponse *(NSURLSession *session, NSURLSessionDataTask *dataTask, NSCachedURLResponse *proposedResponse)
{
NSHTTPURLResponse *resp = (NSHTTPURLResponse*)proposedResponse.response;
NSMutableDictionary *newHeaders = [[resp allHeaderFields] mutableCopy];
if (newHeaders[@"Cache-Control"] == nil) {
newHeaders[@"Cache-Control"] = @"max-age=120, public";
}
NSURLResponse *response2 = [[NSHTTPURLResponse alloc] initWithURL:resp.URL statusCode:resp.statusCode HTTPVersion:nil headerFields:newHeaders];
NSCachedURLResponse *cachedResponse2 = [[NSCachedURLResponse alloc] initWithResponse:response2
data:[proposedResponse data]
userInfo:[proposedResponse userInfo]
storagePolicy:NSURLCacheStorageAllowed];
return cachedResponse2;
}];
});
return sharedInstance;
}
-(instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {
self = [super initWithBaseURL:url sessionConfiguration:configuration];
if (self) {
}
return self;
}
问题是单例无法同时处理 XML 和 JSON 响应。它可以成功解析 JSON 和 XML 1 次。但是当再次调用再次解析时)(成功解析后XML)结果将无法正确解析(给出十六进制字符串响应)
我的JSON 阻止请求
- (void)sendJSONRequest:(NSMutableURLRequest *)request
success:(void (^)(NSURLResponse *response, id responseObject))success
failure:(void (^)(NSError *error))failure {
self.responseSerializer.acceptableContentTypes = [AFHTTPResponseSerializer serializer].acceptableContentTypes;
NSURLSessionDataTask *dataTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error URL: %@",request.URL.absoluteString);
NSLog(@"Error: %@", error);
failure(error);
} else {
NSDictionary *jsonDict = (NSDictionary *) responseObject;
success(response,jsonDict);
NSLog(@"Success URL: %@",request.URL.absoluteString);
NSLog(@"Success %@",jsonDict);
}
}];
[dataTask resume];
}
我的XML 区块请求
- (void)sendXMLRequest:(NSMutableURLRequest *)request
success:(void (^)(NSURLResponse *response, RXMLElement *responseObject))success
failure:(void (^)(NSError *error))failure {
self.requestSerializer = [AFHTTPRequestSerializer serializer];
self.responseSerializer = [AFHTTPResponseSerializer serializer];
[self.responseSerializer.acceptableContentTypes setByAddingObject:@"application/xml"];
NSURLSessionDataTask *dataTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error URL: %@",request.URL.absoluteString);
NSLog(@"Error: %@", error);
failure(error);
} else {
RXMLElement *rootXml = [RXMLElement elementFromXMLData:responseObject];
success(response, rootXml);
NSLog(@"Success URL: %@",request.URL.absoluteString);
NSLog(@"Success %@",rootXml);
}
}];
[dataTask resume];
}
我的十六进制字符串无法响应:
无论如何要使单例正常工作,或者我需要为 JSON 和 XML 响应制作 2 个单例?非常感谢任何帮助。谢谢!
您的错误很可能告诉您它期望 Content-Type
为 application/xml
,而您可能有一个 application/json
。很难说,因为您没有分享完整的错误,但很可能是这样。
有两种合乎逻辑的方法。或者:
让单个 AFHTTPSessionManager
同时接受 XML 和 JSON。因此,使用 AFHTTPResponseSerializer
的 responseSerializer
和 application/xml
和 application/json
的 acceptableContentTypes
(或任何特定的 Content-Type
你的两个网络服务 return),然后当您收到 NSData
响应对象时,根据需要解析 XML 或 JSON。
有单独的 AFHTTPSessionManager
个对象,一个用于 XML(具有 AFXMLResponseSerializer
的 responseSerializer
)和一个用于 JSON(具有responseSerializer
个 AFJSONResponseSerializer
)。注意,如果你这样做,你根本不需要调整acceptableContentTypes
。
您好,我正在为 JSON 和 XML 响应制作一个网络单例
我的ApiClient.h
+ (ApiClient *)sharedInstance;
-(instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration;
我的ApiClient.m
+ (ApiClient *)sharedInstance {
static ApiClient *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:SINGPOST_BASE_URL] sessionConfiguration:sessionConfiguration];
[sharedInstance setDataTaskWillCacheResponseBlock:^NSCachedURLResponse *(NSURLSession *session, NSURLSessionDataTask *dataTask, NSCachedURLResponse *proposedResponse)
{
NSHTTPURLResponse *resp = (NSHTTPURLResponse*)proposedResponse.response;
NSMutableDictionary *newHeaders = [[resp allHeaderFields] mutableCopy];
if (newHeaders[@"Cache-Control"] == nil) {
newHeaders[@"Cache-Control"] = @"max-age=120, public";
}
NSURLResponse *response2 = [[NSHTTPURLResponse alloc] initWithURL:resp.URL statusCode:resp.statusCode HTTPVersion:nil headerFields:newHeaders];
NSCachedURLResponse *cachedResponse2 = [[NSCachedURLResponse alloc] initWithResponse:response2
data:[proposedResponse data]
userInfo:[proposedResponse userInfo]
storagePolicy:NSURLCacheStorageAllowed];
return cachedResponse2;
}];
});
return sharedInstance;
}
-(instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {
self = [super initWithBaseURL:url sessionConfiguration:configuration];
if (self) {
}
return self;
}
问题是单例无法同时处理 XML 和 JSON 响应。它可以成功解析 JSON 和 XML 1 次。但是当再次调用再次解析时)(成功解析后XML)结果将无法正确解析(给出十六进制字符串响应)
我的JSON 阻止请求
- (void)sendJSONRequest:(NSMutableURLRequest *)request
success:(void (^)(NSURLResponse *response, id responseObject))success
failure:(void (^)(NSError *error))failure {
self.responseSerializer.acceptableContentTypes = [AFHTTPResponseSerializer serializer].acceptableContentTypes;
NSURLSessionDataTask *dataTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error URL: %@",request.URL.absoluteString);
NSLog(@"Error: %@", error);
failure(error);
} else {
NSDictionary *jsonDict = (NSDictionary *) responseObject;
success(response,jsonDict);
NSLog(@"Success URL: %@",request.URL.absoluteString);
NSLog(@"Success %@",jsonDict);
}
}];
[dataTask resume];
}
我的XML 区块请求
- (void)sendXMLRequest:(NSMutableURLRequest *)request
success:(void (^)(NSURLResponse *response, RXMLElement *responseObject))success
failure:(void (^)(NSError *error))failure {
self.requestSerializer = [AFHTTPRequestSerializer serializer];
self.responseSerializer = [AFHTTPResponseSerializer serializer];
[self.responseSerializer.acceptableContentTypes setByAddingObject:@"application/xml"];
NSURLSessionDataTask *dataTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error URL: %@",request.URL.absoluteString);
NSLog(@"Error: %@", error);
failure(error);
} else {
RXMLElement *rootXml = [RXMLElement elementFromXMLData:responseObject];
success(response, rootXml);
NSLog(@"Success URL: %@",request.URL.absoluteString);
NSLog(@"Success %@",rootXml);
}
}];
[dataTask resume];
}
我的十六进制字符串无法响应:
无论如何要使单例正常工作,或者我需要为 JSON 和 XML 响应制作 2 个单例?非常感谢任何帮助。谢谢!
您的错误很可能告诉您它期望 Content-Type
为 application/xml
,而您可能有一个 application/json
。很难说,因为您没有分享完整的错误,但很可能是这样。
有两种合乎逻辑的方法。或者:
让单个
AFHTTPSessionManager
同时接受 XML 和 JSON。因此,使用AFHTTPResponseSerializer
的responseSerializer
和application/xml
和application/json
的acceptableContentTypes
(或任何特定的Content-Type
你的两个网络服务 return),然后当您收到NSData
响应对象时,根据需要解析 XML 或 JSON。有单独的
AFHTTPSessionManager
个对象,一个用于 XML(具有AFXMLResponseSerializer
的responseSerializer
)和一个用于 JSON(具有responseSerializer
个AFJSONResponseSerializer
)。注意,如果你这样做,你根本不需要调整acceptableContentTypes
。