RestKit 与 Three20 集成
我正在尝试使用 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>
My应用程序委托代码:
- (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];
}
我的控制器代码:
-(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;
}
并推动控制器:
SecondYouTubeController* viewController = [[SecondYouTubeController alloc] initWithResourcePath:@"/getTop50VideoXML?json=true"];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
首先,我想我需要以某种方式告诉解析器认为视频对象出现在“响应”节点内。 其次,我不确定我是否按照应有的方式拨打了所有电话。
我非常感谢这里的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过将 RKRequestTTModel 上的 keyPath 属性设置为“responses”来深入了解“responses”元素。
您的响应实际上是 XML 吗?目前,RestKit 不支持 XML 有效负载(它已在路线图中重新开始工作)。你能得到一个 JSON 负载吗?如果没有,并且您想打一场漂亮的仗,那么您所需要做的就是实现 RKParser 协议,该协议可以将 XML 与 Cocoa 对象(字典和数组)相互转换。
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).
我发现这个问题希望让 XML 与 RestKit 0.20 一起工作。
在上面的答案时,这是不可能的 - 现在是通过一个附加模块:
https://github.com/RestKit/RKXMLReaderSerialization
您可以通过将其添加到 Podfile 来使用它:
并注册它以供使用:
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:
And registering it for use thus: