Restkit 无法匹配响应描述符

Posted

技术标签:

【中文标题】Restkit 无法匹配响应描述符【英文标题】:Restkit failed to match response descriptors 【发布时间】:2015-09-08 06:43:56 【问题描述】:

这是我的 JSON:link

这是我的映射器:

-(RKObjectManager *)mapContact
    RKObjectMapping* dataMapping = [RKObjectMapping mappingForClass:[Data class]];
    [dataMapping addAttributeMappingsFromDictionary:@
                                                      @"Data": @"app_data",
                                                      @"message": @"app_message",
                                                      @"status":@"app_status",
                                                      @"Success": @"app_success"
                                                      ];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dataMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    RKEntityMapping* contactMapping = [RKEntityMapping mappingForEntityForName:@"Contact" inManagedObjectStore:managedObjectStore];
    contactMapping.identificationAttributes = @[@"con_id"];
    [contactMapping addAttributeMappingsFromDictionary:@
                                                                @"content": @"con_content",
                                                                @"id"     : @"con_id",
                                                                @"title"  : @"con_title",
                                                                @"language":@"con_language",
                                                                ];

    RKResponseDescriptor *responseDescriptor2 = [RKResponseDescriptor responseDescriptorWithMapping:contactMapping
                                                                                             method:RKRequestMethodGET
                                                                                        pathPattern:nil
                                                                                            keyPath:@"data.contentblock"
                                                                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    NSArray *arrResponsDescriptor = [[NSArray alloc]initWithObjects:responseDescriptor,responseDescriptor2,nil];

    [objectManager addResponseDescriptorsFromArray:arrResponsDescriptor];
    return objectManager;


这是我的网络服务调用:

-(void)fetchContactOnCompletion:(myCompletion) compblock
    Mapper *mapper = [Mapper new];
    RKManagedObjectStore *store = [[ASLDataModel sharedDataModel] objectStore];
    NSManagedObjectContext *context = store.mainQueueManagedObjectContext;
    RKObjectManager *objectManager = [mapper mapContact];


    NSString *urlString = [NSString stringWithFormat:@"webservice/content/get/apikey/%@/language/%@/contentid/2",apikey,language];

    NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:urlString parameters:nil];



    RKManagedObjectRequestOperation *operation = [objectManager managedObjectRequestOperationWithRequest:request managedObjectContext:context success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 
        Data *data = [mappingResult.dictionary valueForKey:@""];
        if([data.app_status isEqualToNumber:@200])
            NSError *error = nil;
            BOOL success = [context  save:&error];
            if (!success) RKLogWarning(@"Failed saving managed object context: %@", error);
            NSError *saveError = nil;
            compblock(YES);
        else
            compblock(NO);
        
     failure:^(RKObjectRequestOperation *operation, NSError *error) 

    ];
    [objectManager enqueueObjectRequestOperation:operation];

我得到了这些错误:

2015-09-08 08:38:56.431 ASLTravel[17377:427756] D restkit.object_mapping:RKMapperOperation.m:433 Finished performing object mapping. Results: (null)
2015-09-08 08:38:56.432 ASLTravel[17377:427753] E restkit.network:RKObjectRequestOperation.m:209 GET 'http://asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2' (200 OK / 0 objects) [request=1.6674s mapping=0.0000s total=1.6899s]:
error=Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=0x7f9413f8b730 NSLocalizedFailureReason=A 200 response was loaded from the URL 'http://www.asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2', which failed to match all (2) response descriptors:
  <RKResponseDescriptor: 0x7f9413f535e0 baseURL=http://asl-travel.be/ pathPattern=(null) statusCodes=200-299> failed to match: response URL 'http://www.asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2' is not relative to the baseURL 'http://asl-travel.be/'.
  <RKResponseDescriptor: 0x7f9413f2b1e0 baseURL=http://asl-travel.be/ pathPattern=(null) statusCodes=200-299> failed to match: response URL 'http://www.asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2' is not relative to the baseURL 'http://asl-travel.be/'., NSErrorFailingURLStringKey=http://www.asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2, NSErrorFailingURLKey=http://www.asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2, NSUnderlyingError=0x7f9413c2fc20 "No mappable object representations were found at the key paths searched.", keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded.

谁能帮我解决这个问题?我正在寻找这个问题几个小时......

非常感谢!

【问题讨论】:

【参考方案1】:

Web 服务将您重定向到www.asl-travel.be,因此模式匹配器将永远找不到匹配项。更新基本 URL 或关闭重定向以解决此问题。

【讨论】:

我应该在基本 URL 中准确更改什么。因为我已经尝试过使用或不使用正斜杠? 您目前没有 www 部分

以上是关于Restkit 无法匹配响应描述符的主要内容,如果未能解决你的问题,请参考以下文章

相同 pathPattern 的 RestKit 多个响应描述符

如何在没有响应描述映射的情况下使用 RestKit?

RestKit - 无法在我的 tableView 上显示 JSON 数据

在 Restkit 0.2 中为给定类添加两个请求描述符

Restkit 返回具有 0 个键值对的对象数组

多个对象没有得到保存的核心数据restkit