J'essaie de travailler avec Restkit pour appeler mon API de serveur web, mais les choses ne marchent pas. Mon contrôleur affiche simplement l'indicateur d'activité et rien ne se passe.RestKit avec l'intégration de Three20
J'ai un appel api qui suppose de retourner les 50 meilleures vidéos, par exemple: http://example.com/services/getTop50Video
Le retour est au format:
<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 Code délégué:
- (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 Classe:
@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];
}
Mon code contrôleur:
-(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;
}
et en poussant le contrôleur:
SecondYouTubeController* viewController = [[SecondYouTubeController alloc] initWithResourcePath:@"/getTop50VideoXML?json=true"];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
D'abord, je pense que je dois dire en quelque sorte l'analyseur que la vidéo les objets apparaissent à l'intérieur d'un noeud "response". Deuxièmement, je ne suis pas sûr de faire tous les appels comme il se doit.
J'apprécierais vraiment l'aide ici.
RKRequestTTModel, au moins dans ma source, n'a pas la propriété keyPath. vouliez-vous dire un autre objet? Et oui, j'ai montré l'exemple en XML, mais en fait j'obtiens une réponse JSON. – Idan
non, mais il y a un initialiseur pour cela: - (id) initWithResourcePath: (NSString *) paramètres resourcePath: (NSDictionary *) params objectClass: (Classe) klass keyPath: (NSString *) keyPath; – Jerceratops
Ou, vous pouvez ajouter l'accesseur, assurez-vous juste de le définir avant de définir le modèle en tant que modèle de contrôleurs de vue. – Jerceratops