【发布时间】:2012-07-13 08:26:51
【问题描述】:
好的,我已经尝试解决这个问题大约三周了,我认为我不需要再剃光头了!!!我已尽可能多地解构代码,因此我可以专注于将设备令牌获取到我的数据库中。当我运行代码时,我会在我的数据库中得到一个时间戳记录,就是这样,所以我知道它已连接但什么都没有。
这是我的代码
- (void)application:(UIApplication*)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSString *tokenStr = [deviceToken description];
NSString *pushToken = [[[[tokenStr
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""] retain];
// Save the token to server
NSString *urlStr = [NSString stringWithFormat:ServerApiURL];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"&token=%@",
pushToken] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
[[NSURLConnection alloc] initWithRequest:req delegate:nil];
NSLog(@"%@", pushToken);
NSLog(@"%@", url);
}
这是一个非常简单的 PHP
// initialize connection to database
$db = mysql_connect('localhost', 'xxxxxxxxx', 'xxxxxxxx');
$db_name = "xxxxxxxxxxxx";
mysql_select_db($db_name, $db);
// store incoming data as php variables
error_log(print_r($_POST, true));
$token = $_POST['token'];
// create mysql query
mysql_query("INSERT INTO iPhone (device_token)
VALUES ('$token')");
mysql_query($query);
mysql_close($db);
?>
我在我的数据库中得到的是时间戳,但在 device_token 下没有任何内容。我将不胜感激。谢谢。
【问题讨论】:
-
在
$_POST['token']上使用var_dump并查看其中的内容(很可能是NULL或空字符串'')。数据库很可能不是问题。 -
当我使用 var_dump 它确实返回 NULL。这是什么原因造成的?
标签: php iphone ios database devicetoken