Tôi đang cố gắng làm việc với Restkit để gọi api máy chủ web của tôi, nhưng mọi thứ không hoạt động. Bộ điều khiển của tôi chỉ hiển thị chỉ báo hoạt động và không có gì xảy ra.RestKit với tích hợp Ba20
Tôi có một cuộc gọi api mà giả sử để trả lại 50 video hàng đầu, ví dụ: http://example.com/services/getTop50Video
Sự trở lại có định dạng của:
<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 đại biểu Mã:
- (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];
}
Mã của tôi điều khiển:
-(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;
}
Và đẩy Controller:
SecondYouTubeController* viewController = [[SecondYouTubeController alloc] initWithResourcePath:@"/getTop50VideoXML?json=true"];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
Trước tiên, tôi đoán tôi cần phải bằng cách nào đó nói với các phân tích cú pháp rằng video các đối tượng xuất hiện bên trong nút "phản hồi". Thứ hai, tôi không chắc là tôi đang thực hiện tất cả các cuộc gọi.
Tôi thực sự đánh giá cao trợ giúp tại đây.
RKRequestTTModel, ít nhất trong nguồn của tôi, không có thuộc tính keyPath. bạn có nghĩa là một đối tượng khác không? Và vâng, tôi đã trình bày ví dụ trong xml, nhưng thực ra tôi nhận được phản hồi JSON. – Idan
không, nó không, nhưng có một khởi tạo cho nó: - (id) initWithResourcePath: (NSString *) tham số resourcePath: (NSDictionary *) params objectClass: (Lớp) klass keyPath: (NSString *) keyPath; – Jerceratops
Hoặc, bạn có thể thêm người truy cập, chỉ cần đảm bảo bạn đã đặt nó trước khi bạn đặt mô hình làm mô hình bộ điều khiển chế độ xem. – Jerceratops