通过ASIHTTPRequest JSON从webserver(php / mysql)获取数据

时间:2013-05-15 12:40:03

标签: ios json asihttprequest

我在此功能中向网络服务器发送了reguest:

-(void)getData
{    
    NSURL *url = [NSURL URLWithString:@"http://10.10.10.10/application.php"]; //some address
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    request.delegate = self;
    [request setPostValue:@"6577098" forKey:@"serial_number"];
    [request startAsynchronous];
}

然后在application.php的服务器部分:

<?php
    //$serial_number from $_POST["serial_number"];
    $serial_number = $this->getString('serial_number', 64); 

    $stmt = $this->pdo->prepare('SELECT S.DateTime, SN.Name FROM States S LEFT JOIN StateNames SN on SN.ID=S.State_ID WHERE S.SerialNumber = ?');

    $stmt->execute(array($serial_number));
    $states = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $result = array();
    foreach ($states as $state)
    {               
        $result[] = $state;            
    }

    sendResponse(200, json_encode($result));
?>

当我从浏览器尝试它时,这部分有效,它可以工作,我得到了这个:

[{"DateTime":"2013-05-15 10:22:11","Name":"No water"},{"DateTime":"2013-05-13 14:55:31","Name":"Water"}]

然后回来在Xcode中处理它:

-(void)requestFinished:(ASIHTTPRequest *)request
{
    if (request.responseStatusCode == 200)
    {
       NSString *responseString = [request responseString];
       NSLog(@"%@",responseString);  //if I try this nothing happened
    }
}

我想我必须用JSON做点什么。 我对我的请求反应不好。但我有响应状态码200。 谁能帮我?我没有发现什么是错的。 感谢

1 个答案:

答案 0 :(得分:0)

我需要具体信息来告诉您需要调整哪些服务器设置。我的下一个最佳建议 - 逐行调试。例如在php文件中,执行:

$serial_number = $this->getString('serial_number', 64); //there I get serial_number from $_POST["serial_number"];
echo $serial_number; die;
// ...

此时如果你再次运行你的应用程序,你应该看到序列号打印在 - (void)requestFinished:(ASIHTTPRequest *)请求,如果这里没有任何分解。如果你确实看到它,删除回声并死掉,继续前进

//...
$stmt = $this->pdo->prepare('SELECT S.DateTime, SN.Name FROM States S LEFT JOIN StateNames SN on SN.ID=S.State_ID WHERE S.SerialNumber = ?');

$stmt->execute(array($serial_number));
$states = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($states); die;
// ...

如果没有响应,这可能意味着你的php没有PDO driver installed

如果下一行sendResponse(200, json_encode($result));打破它,那么我的猜测是你的php安装缺少json模块。