如何发布用户电子邮件ID&密码为JSON格式我已经提供了这些信息,但我收到了错误。
如何做这个帖子方法我想要的是如果电子邮件&密码正确,用户必须进入下一个屏幕:
- (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");
}
}
答案 0 :(得分:0)
您只需检查回复结果;
-(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
}
}
有关详细信息,请参阅此answer。