Ios 如何使用NSURL将用户电子邮件密码发布到json我有这些信息,就像我有authenticationtoken一样
如何以JSON格式发布用户电子邮件id和密码我已经给出了这些信息,但我得到了错误 如果电子邮件和密码正确,用户必须进入下一个屏幕:Ios 如何使用NSURL将用户电子邮件密码发布到json我有这些信息,就像我有authenticationtoken一样,ios,iphone,json,Ios,Iphone,Json,如何以JSON格式发布用户电子邮件id和密码我已经给出了这些信息,但我得到了错误 如果电子邮件和密码正确,用户必须进入下一个屏幕: - (IBAction)enterButtonAction:(id)sender { NSString *post =[NSString stringWithFormat:@"user[email]=%@&user[password]=%@",[userNameTF text],[passWordTF text],nil];
- (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");
}
}
您只需检查响应结果
-(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
}
}
欲了解更多信息,看到这一点。我正在等待帮助。您收到了什么错误消息?这不像是错误,我有一个身份验证令牌,通过使用身份验证令牌,我必须验证用户,并允许他下一次屏幕检查requestResponse,如果HTTP状态码为200,这意味着身份验证成功,否则身份验证失败。因此,我必须使用http类