PHPからのデータを表示するiPhoneアプリケーションを開発しています。データはMysqlデータベースから取得され、phpファイルでJSON形式にエンコードされます。
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では次のコードがあります。
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
)です。これを機能させる方法がわかりません...