这里mark下,忙完手里的测试以后写上restful的示例。下面直接说ios下如何使用。
这篇文章写的比较精简。目前只写了get方法。post delete,put 部分下次在补上。
首先需要知道restful走的是json协议的数据。为了测试,需要利用服务器返回一段json。大家可以选用熟悉的方法返回。
这里说一个比较简单的学习测试方法。
mac下面开启webshare功能。也就是网络共享。
(10.9以后需要下载http://clickontyler.com/web-sharing/)
对于老的版本。在系统偏好设置->共享下面应该会有设置。
打开launchpad 输入ter 打开命令行。
输入命令
cd /Library/WebServer/Documents sudo vim articles.json按住快捷键i,复制粘贴内容
{ "articles": [ { "title": "RestKit Object Mapping Intro", "body": "This article details how to use RestKit object mapping...", "author": "Blake Watters", "publication_date": "7/4/2011" }, { "title": "RestKit 1.0 Released", "body": "RestKit 1.0 has been released to much fanfare across the galaxy...", "author": "Blake Watters", "publication_date": "9/4/2011" }] }Esc切换到视图以后。输入
:wq
{ "articles": [ { "title": "RestKit Object Mapping Intro", "body": "This article details how to use RestKit object mapping...", "author": { "name": "Blake Watters", "email": "[email protected]" }, "publication_date": "7/4/2011" }] }
在命令行下输入
gem install cocoapods pod setup等待安装完成
cd ~/Desktop/restkitDemo vim Podfile
platform :ios, '5.0' pod 'RestKit', '~> 0.21.0' pod 'RestKit/Testing', '~> 0.21.0' pod 'RestKit/Search', '~> 0.21.0'
pod install
open restkitDemo.xcworkspace打开项目工程。
@interface Article : NSObject @property (nonatomic, copy) NSString* title; @property (nonatomic, copy) NSString* body; @property (nonatomic, copy) NSString* author; @property (nonatomic) NSDate* publicationDate; @end
#import "DetailViewController.h"
#import "Article.h" #import <RestKit/RestKit.h>
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self.refreshControl beginRefreshing]; [self loadArticles]; }
- (void) loadArticles { RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass:[Article class]]; [articleMapping addAttributeMappingsFromDictionary:@{ @"title": @"title", @"body": @"body", @"author": @"author", @"publication_date": @"publicationDate" }]; RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:articleMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/articles"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]]; [objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { RKLogInfo(@"Load collection of Articles: %@", mappingResult.array); if (!_objects) { _objects = [[NSMutableArray alloc] init]; } [_objects removeAllObjects]; [_objects addObjectsFromArray:mappingResult.array]; [self.refreshControl endRefreshing]; [[self tableView] reloadData]; } failure:^(RKObjectRequestOperation *operation, NSError *error) { RKLogError(@"Operation failed with error: %@", error); }]; [objectRequestOperation start]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; Article *object = _objects[indexPath.row]; cell.textLabel.text = object.title; cell.detailTextLabel.text = object.author; return cell; }
修改loadArticles方法代码为以下代码
RKObjectMapping *authorMapping = [RKObjectMapping mappingForClass:[Author class]]; [authorMapping addAttributeMappingsFromArray:@[@"name", @"email"]]; RKObjectMapping *articleMapping = [RKObjectMapping mappingForClass:[Article class]]; [articleMapping addAttributeMappingsFromDictionary:@{ @"title": @"title", @"body": @"body", @"publication_date": @"publicationDate" }]; [articleMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"author" toKeyPath:@"author" withMapping:authorMapping]]; RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:articleMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"articles" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/articles2"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]]; [objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { RKLogInfo(@"Load collection of Articles: %@", mappingResult.array); if (!_objects) { _objects = [[NSMutableArray alloc] init]; } [_objects removeAllObjects]; [_objects addObjectsFromArray:mappingResult.array]; [self.refreshControl endRefreshing]; [[self tableView] reloadData]; } failure:^(RKObjectRequestOperation *operation, NSError *error) { RKLogError(@"Operation failed with error: %@", error); }]; [objectRequestOperation start];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; Article *object = _objects[indexPath.row]; cell.textLabel.text = object.title; cell.detailTextLabel.text = object.author.name; return cell; }
#import <Foundation/Foundation.h> @interface Author : NSObject @property (nonatomic, copy) NSString* name; @property (nonatomic, copy) NSString* email; @end @interface Article : NSObject @property (nonatomic, copy) NSString* title; @property (nonatomic, copy) NSString* body; @property (nonatomic) Author* author; @property (nonatomic) NSDate* publicationDate; @end
#import "Article.h" @implementation Author @end @implementation Article @end