Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 27th, 2012  |  syntax: None  |  size: 2.10 KB  |  hits: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. consuming restful web service in iOS 5
  2. #import <RestKit/RestKit.h>
  3. @interface MonitorViewController : UIViewController <RKRequestDelegate>
  4.        
  5. RKClient* client = [RKClient clientWithBaseURL:@"http://192.168.2.3:8000/DataRecorder/ExternalControl"];
  6. NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
  7. [client get:@"/json/get_Signals" delegate:self];
  8.        
  9. - (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
  10.     if ([request isGET]) {        
  11.         NSLog (@"Retrieved : %@", [response bodyAsString]);
  12.     }
  13. }
  14.  
  15. - (void) request:(RKRequest *)request didFailLoadWithError:(NSError *)error
  16. {
  17.     NSLog (@"Retrieved an error");  
  18. }
  19.  
  20. - (void) requestDidTimeout:(RKRequest *)request
  21. {
  22.     NSLog(@"Did receive timeout");
  23. }
  24.  
  25. - (void) request:(RKRequest *)request didReceivedData:(NSInteger)bytesReceived totalBytesReceived:(NSInteger)totalBytesReceived totalBytesExectedToReceive:(NSInteger)totalBytesExpectedToReceive
  26. {
  27.     NSLog(@"Did receive data");
  28. }
  29.        
  30. // create the parameters dictionary for the params that you want to send with the request
  31. NSDictionary* paramsDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"00003",@"SignalId", nil];
  32. // send your request
  33. RKRequest* req = [client post:@"your/resource/path" params:paramsDictionary delegate:self];
  34. // set the userData property, it can be any object
  35. [req setUserData:@"SignalId = 00003"];
  36.        
  37. - (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
  38.     // check which request is responsible for the response
  39.     // to achieve this, you can do two things
  40.     // check the parameters of the request like this
  41.     NSLog(@"%@", [request URL]); // this will print your request url with the parameters
  42.     // something like http://myamazingrestservice.org/resource/path?SignalId=00003
  43.     // the second option will work if your request is not a GET request
  44.     NSLog(@"%@", request.params); // this will print paramsDictionary
  45.     // or you can get it from userData if you decide to go this way
  46.     NSString* myData = [request userData];
  47.     NSLog(@"%@", myData); // this will log "SignalId = 00003" in the debugger console
  48. }