5

I'm trying to work with Restkit to call my web server api, but things are not working. My controller just show the activity indicator and nothing happens.

I have an api call which suppose to return the top 50 videos ,for example: http://example.com/services/getTop50Video

The return is in the format of :

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

My Controller code :

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

And pushing the Controller:

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

First, I guess I need to somehow tell the parser that the video objects appear inside a "response" node. Second, I'm not sure I'm doing all the calls as should.

I would really appreciate help here.

benzado
  • 82,288
  • 22
  • 110
  • 138
Idan
  • 5,717
  • 10
  • 47
  • 84

2 Answers2

2

You can drill down into the "responses" element by setting the keyPath property on your RKRequestTTModel to "responses".

Is your response actually XML? Currently RestKit doesn't support XML payloads (it's on the roadmap to get working again). Can you get a JSON payload? If not, and you feel like fighting the good fight, all you need to do is implement the RKParser protocol that can turn XML to/from Cocoa objects (Dictionaries and Arrays).

Jerceratops
  • 389
  • 4
  • 12
  • RKRequestTTModel , at least in my source, does not have keyPath property. did you mean another object ? And yes, I showed the example in xml, but actually I do get a JSON response. – Idan Feb 10 '11 at 17:53
  • no, it doesn't, but there's an initializer for it: - (id)initWithResourcePath:(NSString*)resourcePath params:(NSDictionary*)params objectClass:(Class)klass keyPath:(NSString*)keyPath; – Jerceratops Feb 10 '11 at 19:25
  • Or, you could add the accessor, just make sure you set it before you set the model as the view controllers model. – Jerceratops Feb 10 '11 at 19:26
0

I found this question looking to get XML working with RestKit 0.20.

At the time of the answer above it wasn't possible - it now is via an additional module: https://github.com/RestKit/RKXMLReaderSerialization

You can make use of it by adding this to your Podfile:

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

And registering it for use thus:

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];
Tom
  • 43,583
  • 4
  • 41
  • 61