RestKit与Three20集成

Ida*_*dan 5 iphone rest objective-c three20 restkit

我正在尝试使用Restkit来调用我的Web服务器api,但是事情不起作用.我的控制器只显示活动指示器,没有任何反应.

我有一个api电话,假设要返回前50个视频,例如:http: //example.com/services/getTop50Video

返回的格式为:

<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>
Run Code Online (Sandbox Code Playgroud)

我的应用代表代码:

- (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
}
Run Code Online (Sandbox Code Playgroud)

TWYouTubeVideo类:

@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];
}
Run Code Online (Sandbox Code Playgroud)

我的控制器代码:

-(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;
}
Run Code Online (Sandbox Code Playgroud)

推动控制器:

SecondYouTubeController* viewController = [[SecondYouTubeController alloc] initWithResourcePath:@"/getTop50VideoXML?json=true"];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];    
Run Code Online (Sandbox Code Playgroud)

首先,我想我需要以某种方式告诉解析器视频对象出现在"响应"节点内.其次,我不确定我是否应该做所有的电话.

我真的很感激这里的帮助.

Jer*_*ops 2

您可以通过将 RKRequestTTModel 上的 keyPath 属性设置为“responses”来深入了解“responses”元素。

您的响应实际上是 XML 吗?目前,RestKit 不支持 XML 有效负载(它已在路线图中重新开始工作)。你能得到一个 JSON 负载吗?如果没有,并且您想打一场漂亮的仗,那么您所需要做的就是实现 RKParser 协议,该协议可以将 XML 与 Cocoa 对象(字典和数组)相互转换。