2011-02-10 41 views
5

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.

Trả lời

2

Bạn có thể đi sâu vào phần tử "phản hồi" bằng cách đặt thuộc tính keyPath trên RKRequestTTModel thành "phản hồi".

Phản hồi của bạn có thực sự là XML không? Hiện tại RestKit không hỗ trợ tải trọng XML (trên lộ trình để hoạt động trở lại). Bạn có thể tải trọng JSON không? Nếu không, và bạn cảm thấy như chiến đấu với cuộc chiến tốt, tất cả những gì bạn cần làm là thực hiện giao thức RKParser có thể biến XML thành/từ các đối tượng Cocoa (Từ điển và Mảng).

+0

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

+0

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

+0

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

0

Tôi tìm thấy câu hỏi này đang tìm cách để XML hoạt động với RestKit 0,20.

Tại thời điểm câu trả lời ở trên nó là không thể - nó bây giờ là thông qua một thành phần phụ: https://github.com/RestKit/RKXMLReaderSerialization

Bạn có thể tận dụng nó bằng cách thêm này để Podfile của bạn:

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

Và đăng ký nó để sử dụng như vậy:

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/xml"];