2011-02-10 13 views
5

Estoy tratando de trabajar con Restkit para llamar a mi API del servidor web, pero las cosas no están funcionando. Mi controlador solo muestra el indicador de actividad y no sucede nada.RestKit con integración Three20

Tengo una llamada a la API, que supone que debe devolver los 50 mejores vídeos, por ejemplo: http://example.com/services/getTop50Video

El retorno está en el formato de:

<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> 

Mi Aplicación Código delegado:

- (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 Class:

@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]; 
} 

Mi código Controlador:

-(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; 
} 

y empujando el controlador:

SecondYouTubeController* viewController = [[SecondYouTubeController alloc] initWithResourcePath:@"/getTop50VideoXML?json=true"]; 
[self.navigationController pushViewController:viewController animated:YES]; 
[viewController release];  

En primer lugar, supongo que tengo que decirle de alguna manera el analizador que el video los objetos aparecen dentro de un nodo de "respuesta". En segundo lugar, no estoy seguro de estar haciendo todas las llamadas como debería.

Realmente agradecería ayuda aquí.

Respuesta

2

Puede profundizar en el elemento "respuestas" estableciendo la propiedad keyPath en su RKRequestTTModel en "respuestas".

¿Su respuesta es en realidad XML? Actualmente RestKit no es compatible con XML payloads (está en la hoja de ruta para volver a funcionar). ¿Puedes obtener una carga JSON? De lo contrario, y tiene ganas de pelear la buena batalla, todo lo que necesita hacer es implementar el protocolo RKParser que puede convertir XML en/desde objetos Cocoa (Diccionarios y matrices).

+0

RKRequestTTModel, al menos en mi fuente, no tiene propiedad keyPath. ¿te refieres a otro objeto? Y sí, mostré el ejemplo en xml, pero en realidad obtengo una respuesta JSON. – Idan

+0

no, no es así, pero hay un inicializador para ello: - (id) initWithResourcePath: (NSString *) parámetros de resourcePath: (NSDictionary *) params objectClass: (Clase) klass keyPath: (NSString *) keyPath; – Jerceratops

+0

O bien, puede agregar el acceso, solo asegúrese de configurarlo como modelo de controles de vista. – Jerceratops

0

Encontré esta pregunta buscando que XML funcione con RestKit 0.20.

En el momento de la respuesta anterior no fue posible - que ahora es a través de un módulo adicional: https://github.com/RestKit/RKXMLReaderSerialization

Usted puede hacer uso de ella, añadiendo esto a su PODFILE:

pod 'RKXMLReaderSerialization', :git => 'https://github.com/RestKit/RKXMLReaderSerialization.git', :branch => 'master' 

Y registrarlo para su uso así:

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];