在iOS 5中消费RESTful Web服务 [英] consuming restful web service in iOS 5

查看:126
本文介绍了在iOS 5中消费RESTful Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我第一次的ViewController(MonitorViewController),这是在接口文件MonitorViewController.h:

In my first ViewController (MonitorViewController) this is in the interface file MonitorViewController.h:

#import <RestKit/RestKit.h>
@interface MonitorViewController : UIViewController <RKRequestDelegate>

在MonitorViewController.m viewDidLoad方法中,我有这样的结尾:

In MonitorViewController.m ViewDidLoad method, I have this at the end:

RKClient* client = [RKClient clientWithBaseURL:@"http://192.168.2.3:8000/DataRecorder/ExternalControl"]; 
NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
[client get:@"/json/get_Signals" delegate:self];

的委托方法MonitorViewController.m的实现:

The implementation of delegate methods in MonitorViewController.m:

- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
    if ([request isGET]) {        
        NSLog (@"Retrieved : %@", [response bodyAsString]);
    }
}

- (void) request:(RKRequest *)request didFailLoadWithError:(NSError *)error
{
    NSLog (@"Retrieved an error");  
}

- (void) requestDidTimeout:(RKRequest *)request
{
    NSLog(@"Did receive timeout");
}

- (void) request:(RKRequest *)request didReceivedData:(NSInteger)bytesReceived totalBytesReceived:(NSInteger)totalBytesReceived totalBytesExectedToReceive:(NSInteger)totalBytesExpectedToReceive
{
    NSLog(@"Did receive data");
}

我的AppDelegate方法DidFinishLaunchingWithOptions方法只返回YES,没有别的。

My AppDelegate method DidFinishLaunchingWithOptions method only returns YES and nothing else.

推荐答案

我建议使用 RestKit框架。随着restkit,你只需做:

I recommend using RestKit framework. With restkit, you simply do:

// create the parameters dictionary for the params that you want to send with the request
NSDictionary* paramsDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"00003",@"SignalId", nil];
// send your request
RKRequest* req = [client post:@"your/resource/path" params:paramsDictionary delegate:self];
// set the userData property, it can be any object
[req setUserData:@"SignalId = 00003"];

,然后在委托方法:

And then, in the delegate method:

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    // check which request is responsible for the response
    // to achieve this, you can do two things
    // check the parameters of the request like this
    NSLog(@"%@", [request URL]); // this will print your request url with the parameters
    // something like http://myamazingrestservice.org/resource/path?SignalId=00003
    // the second option will work if your request is not a GET request
    NSLog(@"%@", request.params); // this will print paramsDictionary
    // or you can get it from userData if you decide to go this way
    NSString* myData = [request userData];
    NSLog(@"%@", myData); // this will log "SignalId = 00003" in the debugger console
}

所以你永远不会需要发送未在服务器端使用,只是为了区分您请求的参数。此外, RKRequest 类有很多其他属性,你可以用检查哪些请求对应于特定的反应。但是,如果你发了一堆相同的请求,我想的用户数据的是最好的解决方案。

So you will never need to send the parameters that are not used on the server side, just to distinguish your requests. Additionally, the RKRequest class has lots of other properties that you can use to check which request corresponds to the given response. But if you send a bunch of identical requests, I think the userData is the best solution.

RestKit也将帮助您与其他常见的REST接口的任务。

RestKit will also help you with other common rest interface tasks.

这篇关于在iOS 5中消费RESTful Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆