我的应用程序正在调用一个输出 JSON 数据的基本 PHP 脚本。
我这样调用我的 PHP 脚本:
NSString *buildURL = [NSString stringWithFormat: @"http://xxxx.com/api.php?action=authenticate_user&email=%@&password=%@&deviceToken=%@", _emailAddress, _password, deviceToken];
NSURL *url = [NSURL URLWithString: buildURL];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *ret = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// Parse the result
NSString *ret2 = [ret stringByReplacingOccurrencesOfString:@"null" withString:@""];
NSError* error;
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [ret2 dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &error];
NSString* status = [JSON objectForKey:@"status"];
NSString* email = [JSON objectForKey:@"email"];
NSString* userID = [JSON objectForKey:@"userID"];
NSString* agentID = [JSON objectForKey:@"agentID"];
NSString* picture = [JSON objectForKey:@"picture"];
出于某种原因,在我的 iPhone 上运行此代码时,所有变量都是(null)
,但在模拟器中我得到了正确的数据。
我的 php 脚本的输出是:
{"status":"1", "agentName":"Bill", "picture":"http://xxxx.com/thumb_ad1-8908968097.jpg", "departmentName":"", "email":"test@gmail.com", "agentID":"513", "userID":"3"}null
任何想法我做错了什么?