2016-08-19 59 views
1

您好我正在做一个单身为JSON和XML响应不能让单身的JSON和AFNetworking 3

我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一次。但是,当再次调用再次解析)(后成功地解析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]; 
} 

我的十六进制字符串失败反应: enter image description here

反正让单身正常工作或我需要2单对于j SON和XML响应?任何帮助非常感谢。谢谢!

+0

只显示我们的十六进制字符串失败是不够的,因为这只是向我们展示了响应的JSON正文。你也应该分享错误信息。 – Rob

回答

1

您的错误很可能会告诉您,它期望Content-Typeapplication/xml,并且您可能有application/json。很难说,因为你没有分享完整的错误,但可能是这样的。

有两种逻辑方法。可以是:

  1. 有单个AFHTTPSessionManager接受XML和JSON。因此,的AFHTTPResponseSerializeracceptableContentTypesapplication/xmlapplication/json(或任何具体的Content-Type您的两个Web服务都会返回),然后当您收到NSData响应对象时,或者根据需要解析XML或JSON。

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

+0

非常感谢您的帮助。我设法使用1个单例AFHTTPSessionManager。诀窍是我们需要设置正确的XML或JSON请求和响应序列化程序:self.requestSerializer = [AFJSONRequestSerializer serializer];对于JSON和self.requestSerializer = [AFHTTPRequestSerializer serializer];对于XML。你知道我们可以使用2或3个基本URL吗? –

+0

不要担心基本URL。如果会话正在访问单个服务器,那么这很有用,否则,请不要使用基本URL并提供请求的完整URL,并且它会将基本URL排除在等式之外。 – Rob

+0

那么基本URL是做什么的?我认为它用于更快地连接或为相同的连接保存一些信息? –