Created
July 2, 2013 05:03
-
-
Save jonfriskics/5906885 to your computer and use it in GitHub Desktop.
AFNetworking GET and POST example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface ViewController : UIViewController | |
@property (strong, nonatomic) UILabel *getResponseLabel; | |
@property (strong, nonatomic) UILabel *postResponseLabel; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
#import "AFHTTPClient.h" | |
#import "AFHTTPRequestOperation.h" | |
@implementation ViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
UIButton *getButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; | |
getButton.frame = CGRectMake(20, 20, 280, 40); | |
[getButton setTitle:@"perform GET request" forState:UIControlStateNormal]; | |
[getButton addTarget:self action:@selector(performGet:) forControlEvents:UIControlEventTouchUpInside]; | |
[self.view addSubview:getButton]; | |
UIButton *postButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; | |
postButton.frame = CGRectMake(20, 80, 280, 40); | |
[postButton setTitle:@"perform POST request" forState:UIControlStateNormal]; | |
[postButton addTarget:self action:@selector(performPost:) forControlEvents:UIControlEventTouchUpInside]; | |
[self.view addSubview:postButton]; | |
self.getResponseLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 130, 280, 40)]; | |
[self.view addSubview:self.getResponseLabel]; | |
self.postResponseLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 190, 280, 40)]; | |
[self.view addSubview:self.postResponseLabel]; | |
} | |
- (void)performGet:(id)sender | |
{ | |
// create the baseURL | |
NSURL *baseURL = [NSURL URLWithString:@"http://jonfriskics.com/"]; | |
// alloc init the AFHTTPClient for your baseURL | |
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; | |
// setup the request to use the GET method for a specific API endpoint (I just made this one up) and don't send any | |
// parameters, because for a GET request you would probably just have those in the URL if there were any | |
NSURLRequest *request = [httpClient requestWithMethod:@"GET" | |
path:@"http://jonfriskics.com/afnetworking/get" | |
parameters:nil]; | |
// setup the AFHTTPRequestOperation | |
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; | |
// need to let the httpClient know that we're performing an AFHTTPRequestOperation | |
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; | |
// since we want a JSON response back, we'll set this header | |
[httpClient setDefaultHeader:@"Accept" value:@"application/json"]; | |
// perform the operation | |
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { | |
// take the JSON response object and put it in an NSDictionary called JSON | |
NSError *error; | |
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject | |
options:0 | |
error:&error]; | |
// set the contents of the getResponseLabel to the JSON dictionary | |
self.getResponseLabel.text = [NSString stringWithFormat:@"%@",JSON]; | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
NSLog(@"error: %@",error.localizedDescription); | |
}]; | |
[operation start]; | |
} | |
- (void)performPost:(id)sender | |
{ | |
NSURL *baseURL = [NSURL URLWithString:@"http://jonfriskics.com/"]; | |
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; | |
// everything is the same for POST, except the method is POST, the path is a different endpoint | |
// and a dictionary of parameters is sent to the server | |
// in this case, I'm sending an id parameter with a value of 3, but this really simple endpoint that | |
// I've set up for this example accepts 1, 2, or 3. | |
NSURLRequest *request = [httpClient requestWithMethod:@"POST" | |
path:@"http://jonfriskics.com/afnetworking/post/" | |
parameters:@{@"id":@"3"}]; | |
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; | |
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; | |
[httpClient setDefaultHeader:@"Accept" value:@"application/json"]; | |
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSError *error; | |
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject | |
options:0 | |
error:&error]; | |
self.postResponseLabel.text = [NSString stringWithFormat:@"%@",JSON]; | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
NSLog(@"error: %@",error.localizedDescription); | |
}]; | |
[operation start]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment