Próbuję pracować z programem Restkit, aby wywołać mój api serwera WWW, ale rzeczy nie działają. Mój kontroler pokazuje wskaźnik aktywności i nic się nie dzieje.RestKit z integracją Three20
Mam wywołanie API, które rzekomo zwrócić top 50 filmów, na przykład: http://example.com/services/getTop50Video
Zwrot jest w formacie:
<results>
<mysql_host>72.9.41.97</mysql_host>
<results>
<title/>
<views/>
<video_id>j2xFxHgENt4</video_id>
<thumbnail>http://img.youtube.com/vi/j2xFxHgENt4/2.jpg</thumbnail>
<url/>
</results>
...
</results>
My App delegat Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Configure RestKit Object Manager
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://example.com/services"];
RKObjectMapper* mapper = objectManager.mapper;
[mapper registerClass:[YouTubeVideo class] forElementNamed:@"video"];
// Other non relevant stuff
}
TWYouTubeVideo klasa:
@implementation TWYouTubeVideo
@synthesize title = _title;
@synthesize numberOfViews = _numberOfViews;
@synthesize videoID = _videoID;
@synthesize thumbnailURL = _thumbnailURL;
+ (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"title", @"title",
@"views", @"numberOfViews",
@"video_id", @"videoID",
@"thumbnail", @"thumbnailURL",
nil];
}
Mój kod Kontroler:
-(id) initWithResourcePath:(NSString*) requestedResourcePath
{
if (self = [super init])
{
self.resourcePath = requestedResourcePath;
}
return self;
}
- (void)createModel {
self.model = [[[RKRequestTTModel alloc] initWithResourcePath:self.resourcePath] autorelease];
}
- (void)didLoadModel:(BOOL)firstTime {
[super didLoadModel:firstTime];
RKRequestTTModel* model = (RKRequestTTModel*)self.model;
NSMutableArray* items = [NSMutableArray arrayWithCapacity:[model.objects count]];
for (YouTubeVideo* video in model.objects) {
TableSubtitleItem *item = [TableSubtitleItem itemWithText:video.title
subtitle:[NSString stringWithFormat:@"%@ views", video.numberOfViews]
imageURL:video.thumbnailURL
defaultImage:[YouTubeVideo defaultThumbnail]
URL:nil
accessoryURL:nil];
[items addObject:item];
}
// Ensure that the datasource's model is still the RKRequestTTModel;
// Otherwise isOutdated will not work.
TTListDataSource* dataSource = [TTListDataSource dataSourceWithItems:items];
dataSource.model = model;
self.dataSource = dataSource;
}
i przesuwając kontroler:
SecondYouTubeController* viewController = [[SecondYouTubeController alloc] initWithResourcePath:@"/getTop50VideoXML?json=true"];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
Po pierwsze, myślę, że trzeba jakoś powiedzieć parsera, że film obiekty pojawiają się w węźle "odpowiedzi". Po drugie, nie jestem pewien, czy wykonuję wszystkie połączenia, tak jak powinno.
Naprawdę doceniam pomoc tutaj.
RKRequestTTModel, przynajmniej w moim źródle, nie ma właściwości keyPath. miałeś na myśli inny obiekt? I tak, pokazałem przykład w xml, ale tak naprawdę otrzymałem odpowiedź JSON. – Idan
Nie, nie ma, ale istnieje inicjator dla niego: - (id) initWithResourcePath: (NSString *) parametry resourcePath: (NSDictionary *) params objectClass: (Class) klass keyPath: (NSString *) keyPath; – Jerceratops
Lub możesz dodać akcesor, po prostu upewnij się, że ustawiłeś go przed ustawieniem modelu jako modelu kontrolerów widoku. – Jerceratops