请求Http Url

时间:2016-01-06 08:43:12

标签: php android

我只想调用script.php来控制数据库,就像轮询一样。

这是我的代码,

  

但它给出了错误:无法解析HttpClient。

HttpClient httpclient = new DefaultHttpClient();
httpclient.execute(new HttpGet("http://www.holobech.com/script.php"));  

这里也是我的script.php,它检查时间并每天创建一个表:

<?php
require "includes/database.php";
require "classes/C_UserList.php";
require "classes/C_GlobalClass.php";

$current_game = GlobalClass::fetchinfo("value", "info", "name", "current_game");

$today = date("Y-m-d H:i:s");
$objUserList = new UserList;
$game_started_at = $objUserList->getTotalTime($current_game);

$diff = abs(strtotime($game_started_at . "+1 days") - strtotime($today)); 
$one_day = 60*60*24;
if ($diff >= $one_day) {
    $db_connect = Database::connect();
    $old_game = GlobalClass::fetchinfo("value", "info", "name", "current_game");
    $new_game = $old_game + 1;
    $sql = "UPDATE info SET value = ? WHERE name = ?";
    $query = $db_connect->prepare($sql);
    $query->execute(array($new_game, "current_game"));

    $sql = "CREATE TABLE game".$new_game."(
                id INT AUTO_INCREMENT PRIMARY KEY,
                uid INT NOT NULL,
                highlighting INT NOT NULL,
                sent_who INT NOT NULL,
                sent_amount VARCHAR(255) NOT NULL,
                sent_at DATETIME DEFAULT NULL,
                joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )";
    $query = $db_connect->prepare($sql);
    $query->execute();

    $sql = "SELECT * FROM game".$old_game;
    $query = $db_connect->query($sql, PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        $rows = $query->fetchAll();
        if (count($rows) == 1) {
            $sql = "INSERT INTO game".$new_game." SET uid = ?, highlighting = ?";
            $query = $db_connect->prepare($sql);
            $query->execute(array($rows[0]["uid"], $rows[0]["highlighting"]));
            $game_card = 1;
            $highlight_card = $rows[0]["highlighting"];
        } else {
            for ($i = 0; $i < count($rows); $i++) {
                $sql = "SELECT played FROM user_game_info WHERE uid = ?";
                $query = $db_connect->prepare($sql);
                $query->execute(array($rows[$i]["uid"]));
                $played = $query->fetch();
                $sql = "UPDATE user_game_info SET at_game = ?, game_card = ?, played = ? WHERE uid = ?";
                $query = $db_connect->prepare($sql);
                $query->execute(array("0", "0", $played["played"], $rows[$i]["uid"]));      
            }
            $game_card = 0;
            $highlight_card = 0;
        }
    } else {
        $game_card = 0;
        $highlight_card = 0;
    }

    $sql = "INSERT INTO games SET game_id = ?, money_amount = ?, game_card = ?, highlight_card = ?, started_at = NOW()";
    $query = $db_connect->prepare($sql);
    $query->execute(array($new_game, "0", $game_card, $highlight_card));
}
?>

1 个答案:

答案 0 :(得分:2)

Apache Http已弃用。 添加

  useLibrary 'org.apache.http.legacy'

在defaultConfig中app的build.gradle文件中使用apache httpclient。