0

How to post user email id & password in to JSON format i have given this information but i am getting error.

How to do this post method i want is if email & password is correct user have to enter to the next screen:

- (IBAction)enterButtonAction:(id)sender {
        NSString *post =[NSString stringWithFormat:@"user[email]=%@&user[password]=%@",[userNameTF text],[passWordTF text],nil];

        NSLog(@"PostData: %@",post);

        NSURL *url=[NSURL URLWithString:@"http:secure.sample.in/login"];
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setValue:@"vz_Bim-_QYyzgJBmv68R" forHTTPHeaderField:@"authentication_token"];

        [request setHTTPBody:postData];

        NSURLResponse *requestResponse;
        NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];

        id dict = [NSJSONSerialization JSONObjectWithData:requestHandler options:0 error:nil];

        NSString *id1 = [dict valueForKey:@"id"];
        NSLog(@"id: %@", id1);

        NSString *email = [dict valueForKey:@"email"];
        NSLog(@"email: %@", email);

        NSString *role = [dict valueForKey:@"role"];
        NSLog(@"role: %@", role);

        NSString *phone= [dict valueForKey:@"phone"];
        NSLog(@"phone: %@", phone);

        NSString *full_name = [dict valueForKey:@"full_name"];
        NSLog(@"full_name: %@", full_name);

        NSString *gender = [dict valueForKey:@"gender"];
        NSLog(@"gender: %@", gender);

        if (!requestResponse) {
            NSLog(@"No Request");
        } else {
            NSLog(@"Success");
        }
    }
cyberlobe
  • 1,783
  • 1
  • 18
  • 30
Mahesh M
  • 75
  • 10

1 Answers1

0

You just have to check the response result;

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{   
    if ([response isKindOfClass:[NSHTTPURLResponse class]])
    {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
        //If you need the response, you can use it here
    }
}

For more info, see this answer.

Community
  • 1
  • 1
LoVo
  • 1,856
  • 19
  • 21