RestKit 0.20 无法从服务器获取数据
Posted
技术标签:
【中文标题】RestKit 0.20 无法从服务器获取数据【英文标题】:RestKit 0.20 can't get data from server 【发布时间】:2014-11-02 12:35:36 【问题描述】:我是 Core Data 和 RestKit 的新手,我已经阅读了很多关于 Core Data 和 RestKit 的教程,但我仍然无法从服务器获取数据。在我的应用程序中,我需要获取数据,将其存储在设计中并从数据库加载(如果有或没有互联网连接),我知道 RestKit 可以为我做到这一点,但是当我尝试从 http://salatiki.com.ua/api/get.php?getSaladsType 获取数据时我看到一些错误,但没有任何反应,请帮助我,我做错了什么?我将非常感谢任何有用的参考资料。谢谢。
我有带有“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
在 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;
日志:
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
【问题讨论】:
【参考方案1】:您将难以处理http://salatiki.com.ua/api/get.php?getSaladsType
的 URL,因为您在查询参数中赋予了很多意义,而 RestKit 不将查询参数用于响应描述符模式。
如果您只对这一请求使用get.php
,那么您可以从响应描述符路径模式中删除?getSaladsType
,它应该可以工作。如果您将它用于多种用途,那么您需要停止使用 RestKit,更改服务器以便不使用类似的查询参数或编辑 RestKit...
【讨论】:
【参考方案2】:最后我找到了解决方案,所有问题都在RKResponseDescriptor
,pathPattern
在我的情况下应该为零,我不知道为什么,但它有效!
【讨论】:
以上是关于RestKit 0.20 无法从服务器获取数据的主要内容,如果未能解决你的问题,请参考以下文章
Restkit 0.20 对象在获取 json 响应后未映射