私はRestkitを使ってWebサーバーAPIを呼び出そうとしていますが、機能していません。 私のコントローラはアクティビティインジケータを表示するだけで何も起こりません。RestKit with Three20 Integration
は、私には、例えば、トップ50の動画を返すと仮定API呼び出しを持っている: 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>
私のアプリのデリゲートコード:
- (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クラス:
@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];
まず、私は何とかパーサを伝える必要があります推測している動画オブジェクトは「応答」ノード内に表示されます。 第二に、私はすべての呼び出しを行う必要があるとは確信していません。
本当にありがとうございます。
少なくとも私のソースでは、RKRequestTTModelにはkeyPathプロパティがありません。別のオブジェクトを意味しましたか?そして、はい、私はxmlの例を示しましたが、実際にはJSON応答を取得します。 – Idan
いいえ、それはありませんが、初期化子があります: - (id)initWithResourcePath:(NSString *)resourcePath params:(NSDictionary *)params objectClass:(クラス)klass keyPath:(NSString *)keyPath; – Jerceratops
または、アクセサを追加することもできます。モデルをビューコントローラモデルとして設定する前に、アクセサを追加することができます。 – Jerceratops