简体   繁体   English

RestKit 0.20无法从服务器获取数据

[英]RestKit 0.20 can't get data from server

I'm new in Core Data and in RestKit, I had already read many tutorials about Core Data and about RestKit, but i still can't get my data from server. 我是Core Data和RestKit的新手,我已经阅读了许多有关Core Data和RestKit的教程,但是我仍然无法从服务器获取数据。 In my app i need to get data, store it on devise and load in from DB if there is or there is no internet connection, i know that RestKit can do that for me, but when i try to get my data from http://salatiki.com.ua/api/get.php?getSaladsType i see some errors and nothing happens, please help me, what i'm doing wrong? 在我的应用程序中,我需要获取数据,将其存储在设备上,如果有或没有互联网连接,则可以从DB加载数据,我知道RestKit可以为我做到这一点,但是当我尝试从http:/获取数据时/salatiki.com.ua/api/get.php?getSaladsType我看到一些错误,什么也没有发生,请帮助我,我在做什么错? I will be very grateful for any useful references. 对于任何有用的参考,我将不胜感激。 Thank you. 谢谢。

i have Model.xxdatamodelid with "SCategory" entity 我有带有“ SCategory”实体的Model.xxdatamodelid

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface SCategory : NSManagedObject

@property (nonatomic, retain) NSString * cName;
@property (nonatomic, retain) NSNumber * cId;
@property (nonatomic, retain) NSString * cSection;
@property (nonatomic, retain) NSString * cImage;

@end

in AppDelegate.m 在AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
    [RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];
    NSError *error = nil;
    BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
    if (! success) {
        RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
    }
    NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Store.sqlite"];
    NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
    if (! persistentStore) {
        RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
    }
    [managedObjectStore createManagedObjectContexts];

    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://salatiki.com.ua"]];
    manager.managedObjectStore = managedObjectStore;

    RKEntityMapping *categoryMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([SCategory class]) inManagedObjectStore:managedObjectStore];
    [categoryMapping addAttributeMappingsFromDictionary:@{ @"img": @"cImage",
                                                           @"section": @"cSection",
                                                           @"vid" : @"cId",
                                                           @"vname" : @"cName" }];

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping method:RKRequestMethodGET pathPattern:@"/api/get.php?getSaladsType" keyPath:nil statusCodes:statusCodes];

    [[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
    [manager getObjectsAtPath:@"/api/get.php?getSaladsType" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        NSLog(@"map arr: %@", mappingResult.array);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"request error");
    }];

    // Override point for customization after application launch.
    return YES;
}

log: 日志:

    014-11-02 13:31:41.742 Salatiki[2000:3713] E restkit.network:RKObjectRequestOperation.m:213 GET 'http://salatiki.com.ua/api/get.php?getSaladsType' 
(200 OK / 0 objects) [request=0.0000s mapping=0.0000s total=0.0267s]: Error
 Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the 
response loaded." UserInfo=0x7f9fb883c880 {NSLocalizedFailureReason=A 200 response was 
loaded from the URL 'http://salatiki.com.ua/api/get.php?getSaladsType', which failed to 
match all (0) response descriptors:, NSErrorFailingURLStringKey=http://salatiki.com.ua/api/get.php?getSaladsType, 
NSErrorFailingURLKey=http://salatiki.com.ua/api/get.php?getSaladsType, 
NSUnderlyingError=0x7f9fb883c3c0 "No mappable object representations were found at the 
key paths searched.", keyPath=null, NSLocalizedDescription=No response descriptors match 
the response loaded.}
    2014-11-02 13:31:41.766 Salatiki[2000:613] I restkit.network:RKObjectRequestOperation.m:150 GET 'http://salatiki.com.ua/api/get.php?
getSaladsType'
    2014-11-02 13:31:41.766 Salatiki[2000:613] request error

You're going to struggle with a URL of http://salatiki.com.ua/api/get.php?getSaladsType because you are putting a lot of meaning into the query parameters and RestKit doesn't use query parameters for response descriptor patterns. 您将无法使用http://salatiki.com.ua/api/get.php?getSaladsType的URL,因为您在查询参数中添加了很多含义,而RestKit并未将查询参数用于响应描述符图案。

If you're only using get.php for this one request then you can just remove ?getSaladsType from the response descriptor path pattern and it should work. 如果您仅对这一请求使用get.php ,则只需从响应描述符路径模式中删除?getSaladsType ,它就可以工作。 If you're using it for multiple things then you'll need to stop using RestKit, change the server so you don't use query parameters like that or edit RestKit... 如果您将它用于多种用途,则需要停止使用RestKit,更改服务器,以免使用类似这样的查询参数或编辑RestKit ...

最后,我找到了解决方案,所有问题都在RKResponseDescriptor ,在我的情况下pathPattern应该为nil,我不知道为什么,但是它有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM