Monday, 11 July 2016

Json GET & POST (AFNetworking)

Json GET & POST  Using AFNetworking



Here Below AFNetworking Folder Given Below Which Have All File Of Json  ....


Click Here To Download :-   Download


This Folder Must Be Import in Your Bundle ....


Write Below Code In Info.plist :-


<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>



Write Coding in ViewController.m File ...

#import "ViewController.h"
#import "AFNetworking.h"

@interface ViewController ()
{
    NSDictionary *ResponceDict ;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(IBAction)GET_Click:(id)sender
{
    

    
    //-----------------------------
    
    // send request for schools
    
    NSString *strURL = [NSString stringWithFormat:@"YOUR URL"] ;
    NSURL *url = [NSURL URLWithString:strURL] ;
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 2
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    //operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        // 3
        ResponceDict = (NSDictionary *)responseObject;
        
        NSLog(@"%@",ResponceDict) ;
        
       
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        // 4
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
        
       
        
    }];

    
    // 5
    [operation start];
    
    
}


-(IBAction)POST_Click:(id)sender
{
    //---- send request for get profile [POST]
    
    NSDictionary *params = @{@"user_id" : @"7"
                             };
    
    NSString* HOST_URL = @"YOUR URL";
    
    AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
    operationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    [operationManager POST:HOST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
        
        // Enter what happens here if successsful.
        
        ResponceDict = (NSDictionary *)responseObject;
        
        NSLog(@"%@",ResponceDict) ;
            
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        
        // Enter what happens here if failure happens
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
        
        
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

No comments:

Post a Comment