这是我的PHP代码:
header('Content-type: application/json');
$con = mssql_connect('xx.xx.xx.xx\\MSSQLSERVER2012', 'user', 'password');
@mssql_select_db('mydbname') or die ("Error");
$username = $_POST['username'];
$query = "SELECT username,password FROM users WHERE username='$username'";
$result = mssql_query($query, $con) or die ("Error");
$num = mssql_num_rows($result);
$rows = array();
while ($r = mssql_fetch_assoc($result))
{
$rows[] = $r;
if($_POST['username'] == $username)
{
echo '{"success":1}';
}else
{
echo '{"success":0,"error_message":"Username and/or password is invalid."}';
}
}echo json_encode($rows);
mssql_close($con);
和ios代码:
@try {
if([[txtUsername text] isEqualToString:@""] || [[txtPassword text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"KullaniciAdi=%@",[txtUsername text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://mysite/json/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 = [[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);
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
NSLog(@"%d",success);
if(success == 1)
{
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!"];
}
如果我使用mysql数据库工作正常,但msssql不行。如何从mssql中获取我的项目。 我的json数据日志:
[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x8c8bf50
如果我在php中删除了json编码,则响应为null。
答案 0 :(得分:0)
如果没有看到你的mssql数据库发送的jsonBody,很难分清楚是怎么回事,但如果我不得不猜测,你就会因为这条线而遇到这个问题。
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
查看SBJson库,选择器objectWithString:可以返回NSDictionary或NSArray,所以如果你的json有效负载如下所示:
[{"record":1,"value":"record1"},{"record":2,"value":"record2"}]
然后objectWithString将返回一个数组,以便在您调用
时NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
你得到一个崩溃,用于将NSDictionary的选择器发送到NSArray。尝试这样的事情:
id parsedObject = [jsonParser objectWithString:responseData error:nil];
if ([parsedObject isKindOfClass:[NSDictionary class]]) {
//Treat as a NSDictionary
} else if ([jsonData isKindOfClass:[NSArray class]]) {
//Treat as an array of NSDictionaries
}