我试图制作登录页面,通过Json从php文件中获取数据。它工作正常,直到我使用这句话[jsonData objectForKey:@" state"],出现异常。我试图让另一个dictonary像之前的一些问题的答案。但没有任何事情与我合作。
- (IBAction)login:(id)sender {
@try {
NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",[firsttext text],[second text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://itsolution.co.in/coverageapi/login.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [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 setHTTPBody:postData];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %d", [response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"jsondata %@",jsonData);
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
NSLog(@"%d",success);
if(success == 1)
{
NSLog(@"Login SUCCESS");
} else {
NSLog(@"failed");
}
} else {
if (error) NSLog(@"Error: %@", error);
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
}
答案 0 :(得分:0)
试试这个并且请检查状态键是否存在
- (void) logIn {
///1
NSMutableDictionary *responseDic_Lv=[[NSMutableDictionary alloc]init];
NSString *post_Lv = [NSString stringWithFormat:@"email_id=%@&password=%@" ,userNameTFT1.text,passwordTFT2.text];
///2
NSData *postData_Lv = [post_Lv dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength_Lv = [NSString stringWithFormat:@"%lu",(unsigned long)[postData_Lv length]];
//NSLog(@"Post Length Is... %@",postLength_Lv);
//3
NSString *BaseUrl_Login = [NSString stringWithFormat:@"Your_Login_URL"];
NSMutableURLRequest *request_Lv =[[NSMutableURLRequest alloc] init];
[request_Lv setURL:[NSURL URLWithString:BaseUrl_Login]];
//NSLog(@" Rahul Goku request is >>> %@",request);
[request_Lv setHTTPMethod:@"POST"]; //Write Post or Get accordind method here
[request_Lv setValue:postLength_Lv forHTTPHeaderField:@"Content-Length"];
[request_Lv setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request_Lv setHTTPBody:postData_Lv]; //Set HTTP
NSData *response_Lv = [NSURLConnection sendSynchronousRequest:request_Lv
returningResponse:nil error:nil];
responseDic_Lv=[NSJSONSerialization JSONObjectWithData:response_Lv options:NSJSONReadingAllowFragments error:nil];
///4 Chk Data...
NSString *ResultStr1 = (NSString *)[responseDic_Lv valueForKey:@"YourKey"];
NSLog(@"Result From Server is=====> %@",ResultStr1);
if ([ResultStr1 isEqualToString:@"success"]) {
NSString *ResponseStr2 = (NSString *)[responseDic_Lv valueForKey:@"YourKey"];
} else { // Login Error password or email.... Error...
}
}