嗯,RestKit 的初学者,对我来说的第一个例子是在foursquare API 上,我使用的是RestKit 和块而不是委托。
我想检索场地的名称,这是 JSON 响应
这是我的代码:
// 应用代理.m
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURLString:@"https://api.Foursquare.com/v2"];
RKManagedObjectStore *objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"Venue.sqlite"];
objectManager.objectStore = objectStore;
objectManager.serializationMIMEType = RKMIMETypeJSON;
RKManagedObjectMapping *venueMapping = [RKManagedObjectMapping mappingForClass:[Venue class] inManagedObjectStore:objectStore];
[venueMapping mapKeyPath:@"id" toAttribute:@"id"];
[venueMapping mapKeyPath:@"name" toAttribute:@"name"];
venueMapping.primaryKeyAttribute = @"id";
[objectManager.mappingProvider setMapping:venueMapping forKeyPath:@"response.venue"];
然后在 myViewController.m
-(void)loadVenues{
// When caling loadObjectsAtResourcePath method it specify RKObjectLoader which is the actual request.
// within these block you can take more options to controll the request.
[[RKObjectManager sharedManager]loadObjectsAtResourcePath:@"/venues/40a55d80f964a52020f31ee3?oauth_token=FNQPN5P5EKLJ5IQ44TMWO00I3W033M0Y1TKINW2OTF2VIOTP&v=20130512" usingBlock:^(RKObjectLoader* loader)
{
loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[Venue class]];
loader.onDidLoadObject = ^(NSArray *objects)
{
NSLog(@"onDidLoadObject Blocks");
self.data = objects;
[self.tableView reloadData];
};
}
];
}
并且应用程序正在进入 onDidLoadObject 块,但每次数组为空!即使我在浏览器上测试链接,它也带有数据。
当我调试 loader.URL 它总是带有这些
https://api.Foursquare.com/v2/venues/40a55d80f964a52020f31ee3?v=20130512&oauth_token=FNQPN5P5EKLJ5IQ44TMWO00I3W033M0Y1TKINW2OTF2VIOTP -- https://api.Foursquare.com/v2 -- https://api.Foursquare.com/v2
我不知道为什么 load.URL 是错误的?!
我认为嗯以错误的方式调用 4square API,有人可以帮忙吗?:)