我正在开发一个iPhone应用程序来显示来自php的数据。从PHP获取JSON数据到iOS
include_once 'connectionIncl.php';
if(function_exists($_GET['method'])){
$_GET['method']();
}
function getSQLIntoJSONFormat()
{
$arr;
$sql = mysql_query("SELECT * FROM pecivo");
while($pecivo = mysql_fetch_assoc($sql)){
$arr[] = $pecivo['typ'];
}
$arr= json_encode($arr);
echo $_GET['jsoncallback'].'('.$arr.')';
}
// --- http://127.0.0.1:8887/TeplyRohlik/pecivo.php?method=getSQLIntoJSONFormat&jsoncallback=?
当我从浏览器运行此,它返回正确的数据:
(["sejra","knir","baba","vousy","sporitelna25"])
此外,iOS上有这样的代码数据被从MySQL数据库然后被编码到JSON格式在PHP文件获得:
NSString * urlString = [NSString stringWithFormat:@"http://192.168.0.10:8887/TeplyRohlik/pecivo.php?method=getSQLIntoJSONFormat&jsoncallback=?"];
NSURL * url = [NSURL URLWithString:urlString];
NSData * data = [NSData dataWithContentsOfURL:url];
NSError * error;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%@",json);
而且结果是....(null
)。 我不知道如何得到这个工作...
如果可以从您的设备访问URL,您尝试过吗?转到您的手机浏览器,然后键入上面的网址,查看它是否正确回答 – RedRoosterMobile 2012-07-21 17:07:39
欢迎来到Stack Overflow!请不要将'mysql_ *'函数用于新代码。他们不再被维护,社区已经开始[弃用流程](http://goo.gl/KJveJ)。请参阅[**红框**](http://goo.gl/GPmFd)?相反,您应该了解[准备好的语句](http://goo.gl/vn8zQ)并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能决定,[本文](http://goo.gl/3gqF9)将有助于选择。如果你关心学习,[这里是很好的PDO教程](http://goo.gl/vFWnC)。 – 2012-07-21 17:07:40
为什么不看看NSError对象来查找更多信息? o,0 – borrrden 2012-07-21 17:10:28