1

我开始开发一个 android 应用程序,它将通过 php 服务使用数据库连接。

我成功安装了 apache 2.2 服务器和 php 5 并配置了它们。我的项目中没有连接问题,但问题是:1)应用程序连接到php服务,执行查询并获取结果,2)但是在一些查询运行后,服务开始没有响应,我只是无法解决它2天。

当服务开始无响应时,我通过在资源管理器中输入 http:/localhost:8888/android_api/(android_api 是包含 php 服务功能的文件夹)来检查它,结果显示为无响应。当我刷新页面时,它开始再次响应。

我的 php 代码(本地主机上的 index.php:8888/android_api):

<?php
/**
 * File to handle all API requests
 * Accepts GET and POST
 *
 * Each request will be identified by TAG
 * Response will be JSON data

/**
 * check for POST request
 */
if (isset($_POST['tag']) && $_POST['tag'] != '') {
    // get tag
    $tag = $_POST['tag'];

    // include db handler
    require_once 'include/DB_Functions.php';
    $db = new DB_Functions();

    // response Array
    $response = array("tag" => $tag, "success" => 0, "error" => 0);

    // check for tag type
    if ($tag == 'insert') {
        // Request type is check Login
        $icao = $_POST['icao'];
        $name = $_POST['name'];
        $latitude = $_POST['latitude'];
        $longitude = $_POST['longitude'];

        // check for user
        $result = $db->insertAirport($icao, $name, $latitude, $longitude);
        if ($result) {
            $response["success"] = 1;
        } else {
            $response["error"] = 1;
        }
        echo json_encode($response);
    } else if ($tag == 'getall') {
        $result = $db->getAllAirports();
        $no_of_rows = mysql_num_rows($result);
        if($no_of_rows > 0) {
            $response["success"] = 1;
            while($r = mysql_fetch_assoc($result)) {
                $response[$result["icao"]] = $r;
            }
        } else {
            $response["error"] = 1;
        }
        echo json_encode($response);
    } else {
        echo "Invalid Request";
    }
} else {
    echo "Access Denied";
}
?>

处理连接的android代码:

 try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

url 和 params 变量包含正确的数据。任何帮助都是神一样的。

4

0 回答 0