我正在尝试制作从php
文件获取数据的登录页面Json
。它工作正常,直到我使用这句话[jsonData objectForKey:@"state"]
,出现异常。我试图制作另一个字典,比如之前的一些问题的答案。但没有什么对我有用。
输出 :
Response code : 200
Resonse ==> [{"state":"true"}]
JasonData ({state = true})
然后异常
-[__NSarrayM objectforkey:]: unrecognized selector sent to instance
如果你能帮助我,我是 objctive-C 的新手。我会很开心
- (IBAction)loginClicked:(id)sender
{
@try
{
if([[txtUserName text] isEqualToString:@""] || [[txtPassword text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[txtUserName text],[txtPassword text]];
NSLog(@"PostData: %@",post);
NSURL *url = [NSURL URLWithString:@"http://localhost/services/pages/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];
NSError *error = [[NSError alloc] init];
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);
NSLog(@"%@",[jsonData objectForKey:@"state"]);
NSString *state = [(NSString*) [jsonData objectForKey:@"state"] lowercaseString ];
NSLog(@"%@", state);
if([state isEqualToString:@"true"])
{
NSLog(@"Login SUCCESS");
[self alertStatus:@"Logged in Successfully." :@"Login Success!"];
}
else
{
NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
[self alertStatus:error_msg :@"Login Failed!"];
}
}
else
{
if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Login Failed!"];
}
}
}
@catch (NSException * e)
{
NSLog(@"Exception: %@", e);
[self alertStatus:@"Login Failed." :@"Login Failed!"];
}