本篇主要簡單介紹了一下restkit中的網絡連接,字段映射,與RKTableController。並且在最后給出一個簡單的load more的RKTableController的派生子類。
RestKit連接:https://github.com/RestKit/RestKit/
一.網絡連接
這里主要用到的是RKClient
初始化
- (void)initRKClient { // Initialize with a Base URL RKClient* client = [RKClient clientWithBaseURL:@"http://restkit.org"]; ! // Setup HTTP AUTH 設置http驗證 client.username = @"restkit"; client.password = @"rocks"; ! // Set an app-wide API key HTTP header 設置全局的http頭 [client setValue:@"123456" forHTTPHeaderField:@"X-RESTKIT-API-KEY"]; ! // The first initialized RKClient becomes // the sharedClient instance 檢測網絡 [[RKClient sharedClient] isNetworkAvailable]; }
baseURL的作用是一個網絡請求的域(host),這樣就不用頻繁的寫全整個url,之后的調用只要附上后面的地址就行了,往下看。
- (void)sendRequest { // Send an HTTP GET request to 'http://restkit.org/contacts' 發送一個get請求,完整的請求路徑是“http://restkit.org/contacts” [[RKClient sharedClient] get:@"/contacts" delegate:self]; } // RKRequestDelegate methods代理方法 - (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {// 成功 RKLogInfo(@"Yay! We Got a response"); } - (void)request:(RKRequest*)request didFailLoadWithError:(NSError *)error { // 失敗 RKLogInfo(@"Oh no! We encountered an error: %@", [error localizedDescription]); }
其中[RKClient sharedClient]得到的你第一個生成的RKClient對象。你當然可以不用[RKClient sharedClient],而用你自己另外生成的RKClient對象來訪問網絡,這樣你就可以在一個頁面同時發起多個請求了。
更多的發送post請求參考官方文檔。
二.字段映射
這里主要用到的類是RKObjectManager和RKObjectMapping
用法:比如說你有一個文章列表需要加載,網絡發給你的json是
{"article_list":[
{"title":"article1","title_id":3},{"title":"article2","title_id":4}]}
而你想映射到本地是一個這樣的類結構
Article{ NSString* aTitle; int tid; }
那么首先定義類
@interface Article @property(nonatomic,copy)NSString* aTitle; @property(nonatomic)int tid; @end @implement @synthesize aTitle; @synthesize tid; @end
然后生成一個映射規則 RKObjectMapping 對象
RKObjectMapping *contactMapping = [RKObjectMapping mappingForClass:[Articleclass]]; [contactMapping mapKeyPath:@"title" toAttribute:@"aTitle"]; [contactMapping mapKeyPath:@"title_id" toAttribute:@"tid"];
然后生成一個RKobjectManager對象,實現數據獲取后直接映射成所需類對象
RKObjectManager *manager = [RKObjectManager objectManagerWithBaseURL:@"http:// restkit.org"]; // Ask for & generate JSON json格式數據 manager.acceptMIMEType = RKMIMETypeJSON; manager.serializationMIMEType = RKMIMETypeJSON; [[RKObjectManager sharedManager].mappingProvider setMapping:contactMapping forKeyPath:@"article_list"];
其中baseURL和RKClient中的意義相同,[RKObjectManager sharedManager]也是你第一個生成的RKObjectManager對象。注意最后一句,他為JSON的 @"article_list"這個path綁定了contactMapping這種映射關系。如果你的json不是字典結構,而直接就是數組,那么這里設置path為@“”就可以了。
如果你有多個不同的請求,但是數據都是放在“article_list”這個path下面,那么就不能都是用[RKObjectManager sharedManager],而要為其他的請求另外生成RKObjectManager對象,並且要注意retain,否則可能導致解析失敗。
最后通過這個RKObjectManager來訪問網絡資源就可以了。
- (void)loadRemoteObjects { // GET /contacts 完整路徑是“http:// restkit.org/contacts” [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/contacts" delegate:self]; } - (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects { if ([objectLoader wasSentToResourcePath:@"/contacts"]) { // Introspect the resource path 完成並解析城Article對象保存在objects中 NSLog(@"Nice! We loaded the following contacts: %@", objects); } } - (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error { // Note that failures here can be at the _application_ level in addition to transport 請求失敗 NSLog(@"Rats! Failed to load objects: %@", [error localizedDescription]); }
三. RKTableController
最終的最終,我們請求了數據之后還是為了顯示。RestKit只要短短幾行代碼就可以把我們的數據顯示在tableview中了,這就靠強大的RKTableController
RKTableController *tableController = [VRKTableController tableControllerWithTableView:self.tableView forViewController:self]; // 代理,有開始網絡連接,數據加載完成的相關回調 tableController.delegate = self; // 一行代碼實現下拉刷新 tableController.pullToRefreshEnabled = YES; // 如果沒有額外設置RKTableController的objectManager的話,他默認使用[RKObjectManager sharedManager] [self.tableController mapObjectsWithClass:[Summary class] toTableCellsWithMapping: [RKTableViewCellMapping cellMappingUsingBlock:^(RKTableViewCellMapping* cellMapping) { cellMapping.onSelectCellForObjectAtIndexPath = ^void(UITableViewCell *cell, id object, NSIndexPath *indexPath){ //點擊事件 }; // 行高 cellMapping.rowHeight = 44; // 設置cell的具體數據 cellMapping.onCellWillAppearForObjectAtIndexPath = ^(UITableViewCell* cell, id object, NSIndexPath* indexPath) { Article* article = (Article*)object; cell.textLabel.text = article.title; }; }]]; // 開始加載資源 NSString* resourcePath = [NSString stringWithFormat:@"/content"]; [self.tableController loadTableFromResourcePath:resourcePath];
這些就實現了列表展示了,一切看起來都是那么美好,但是,如何load more呢?我找了半天也沒有找到很好的方法,所有就自己派生了一個。附上下載
