Tuesday, 1 April 2014

Getting Data from server in json format using POST method


-(void)httpPostWithCustomDelegate
{
    NSError *error;
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL * url = [NSURL URLWithString:@"http://four-developers.com/service1.svc/GetPostData"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    NSMutableDictionary* mapData = [[ NSMutableDictionary alloc ] init ];
    [ mapData setObject :@"aa" forKey : @"value" ];
    NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
    [request setHTTPBody:postData];
    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
        NSError *err = nil ;
        
        NSDictionary   *responseDict = [ NSJSONSerialization JSONObjectWithData :data options : 0 error :&err];
        
        NSDictionary *dict = [responseDict objectForKey : @"GetPostDataResult" ];
        NSLog(@"%@",dict);
    }];
    
    [postDataTask resume];
    
}

        OR


Note: before starting coding for retreiveDataFromPOST you must be need to import afnetworking library...


OR

http://four-developers.com/documents/UsingAFNetworking.zip


-( void )retreiveDataFromPOST
{
    NSURL *url = [[ NSURL alloc ] initWithString :@"http://four-developers.com/service1.svc/GetPostData"];
    AFHTTPClient *httpClient = [[ AFHTTPClient alloc ] initWithBaseURL :url];
    
    [httpClient setParameterEncoding : AFJSONParameterEncoding ];
    
    [httpClient registerHTTPOperationClass :[ AFHTTPRequestOperation class ]];
    
    [httpClient setDefaultHeader : @"Content-Type" value : @"application/json" ];
    
    NSMutableDictionary* LoginDict = [[ NSMutableDictionary alloc ] init ];
    [ LoginDict setObject :@"aa" forKey : @"value" ];
    
    NSMutableURLRequest *request = [httpClient requestWithMethod : @"POST"
                                    
                                                            path :[url path ]
                                    
                                                      parameters : LoginDict ];
    
    [request setTimeoutInterval : 200 ];
    
    AFHTTPRequestOperation *operation = [[ AFHTTPRequestOperation alloc ] initWithRequest :request];
    
    [operation setCompletionBlockWithSuccess :^( AFHTTPRequestOperation *operation, id responseObject) {
        
        // Print the response body in text
        
        NSError *err = nil ;
        
        NSDictionary   *responseDict = [ NSJSONSerialization JSONObjectWithData :responseObject options : 0 error :&err];
      
        NSDictionary *dict = [responseDict objectForKey : @"GetPostDataResult" ];
        NSLog(@"%@",dict);
       
        
    }
    failure :^( AFHTTPRequestOperation *operation, NSError *error)
     
     {
         
         NSLog ( @"error login %@" ,error);
         UIAlertView *alert = [[ UIAlertView alloc ] initWithTitle : @"Alert" message : @"Network Failure" delegate : nil cancelButtonTitle : @"OK" otherButtonTitles : nil , nil ];
         
         [alert show ];
         
               self . view . userInteractionEnabled = YES ;
     }];
    
    [operation start ];


}

No comments:

Post a Comment