我正在我的 iPhone 应用程序中创建一个登录页面,并使用 Json 调用 PHP 页面以连接到 MySql 数据库。
我与 PHP 页面有联系,但我的程序没有发布任何内容。
- (IBAction)login:(id)sender{
//Alloc dictionary
jsonDictionaryLogin = [[NSDictionary alloc] initWithObjectsAndKeys: securityCode, @"hash_app", loginTextField.text, @"app_email", passwordTextField.text, @"app_password", nil];
NSError *jsonError = nil;
jsonDataLogin = [NSJSONSerialization dataWithJSONObject:jsonDictionaryLogin
options:kNilOptions error:&jsonError];
if(!jsonError) {
NSString *serJSON = [[NSString alloc] initWithData:jsonDataLogin encoding:NSUTF8StringEncoding];
NSLog(@"\n\nSerialized JSON: %@", serJSON);
} else {
NSLog(@"JSON Encoding Failed: %@", [jsonError localizedDescription]);
}
NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com/mypage.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%d", [jsonDataLogin length]] forHTTPHeaderField:@"content-Length"];
[request setHTTPBody:jsonDataLogin];
NSLog(@"jsonData: %@",jsonDataLogin);
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];
[connection start];
}
之后,在我的委托方法中:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData{
NSLog(@"String sent from server %@",[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]);
}
我的PHP代码:
<?
$hash = $_POST['hash_app'];
echo json_encode(array("TEST" => "swrao")); //RETURN swrao
echo json_encode($hash); //RETURN null
if($hash == "string_sended")
{
echo json_encode(array("TEST" => "YOU ARE IN")); //NO RETURN
}
?>
NSLog 中的字符串给出了一些我在 PHP 页面中编写的消息,但是 PHP 页面没有接收到发送的数据。
有什么想法?