无法使用AFNetworking 3为JSON和XML响应创build单例

嗨,我正在做一个单身人士的networkingJSON和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响应。 它可以成功parsingJSON和XML一次。 但是再次调用时再次parsing)(成功parsingXML之后)结果将不会被正确parsing(给出hexstring响应)

我的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]; } 

我的hexstring失败了: 在这里输入图像说明

无论如何,使单身人士正常工作,或者我需要为JSON和XML响应做2单身人士? 任何帮助非常感谢。 谢谢!

你的错误可能告诉你,它正在期待一个Content-Typeapplication/xml ,你可能有一个application/json 。 这很难说,因为你没有分享完整的错误,但可能是这样的。

有两个合乎逻辑的方法。 或者:

  1. 让单个AFHTTPSessionManager接受XML和JSON。 因此,使用AFHTTPResponseSerializerapplication/xmlapplication/json (或任何特定的Content-Type您的两个Web服务返回)的acceptableContentTypes Content-Type ,然后当您收到NSData响应对象时,将XML或JSONparsing为适当。

  2. 有单独的AFHTTPSessionManager对象,一个用于XML(具有AFXMLResponseSerializer ),一个用于JSON(具有AFJSONResponseSerializer )。 请注意,如果您这样做,则根本不需要调整acceptableContentTypes