【发布时间】:2016-10-18 20:31:21
【问题描述】:
我正在尝试制作通过 Json 从 php 文件中获取数据的登录页面。在我使用这句话 [jsonData objectForKey:@"state"] 之前它工作正常,出现异常。我试图制作另一个字典,比如之前的一些问题的答案。但没有什么对我有用。
- (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);
}
}
【问题讨论】:
-
[__NSArrayM objectForKey:]: unrecognized selector简单地说,接收者是一个数组而不是字典。 -
@Dipek 关键“状态”在您的回复中不存在。