0

データベースからデータを取得してAndroidアプリで表示するための簡単なAPIを作成しました

PHP関数のテストに問題があります

関数の例:-

<?php
include 'json_config.php';
function get_city(){
mysql_query("SET NAMES utf8;");
mysql_query("SET CHARACTER_SET utf8;");
$return=array();
//$country_name=$_POST['country_name'];

// get all products from products table
$result = mysql_query("SELECT *FROM city") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["city"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        // Full texts   id  dead_name   masged_name     gender  salah       notes   date    city_name   hour    min 
        $region_subscribt = array();
        $region_subscribt["id"] = $row["id"];
        $region_subscribt["city_name"] = $row["city_name"];
        $region_subscribt["region_name"] = $row["notes"];
        $region_subscribt["notes"] = $row["country_name"];
    // push single product into final response array
    array_push($response["city"], $region_subscribt);
}


// echoing JSON response
echo json_encode($response,JSON_UNESCAPED_UNICODE);
} else {
// no products found
$response["fail"] = 0;


// echo no users JSON
echo json_encode($response,JSON_UNESCAPED_UNICODE);
}
}//end of the function

// get_city();
?>

firefox で poster extension を使用してコンテンツに配置すると、コンテンツに配置するとデータが得られませんでした

 getcity();

しかし、phpファイル内の関数を呼び出すと、データが正しく取得されました!

返された json を別のアプリで使用するために httprequest で値を渡す方法 !

4

1 に答える 1

1

メソッド名をコンテンツではなくパラメーターとして送信し、メソッド名を確認して必要なコードを呼び出す必要があります。

このスナップショットを見る

if(isset($_GET['method_name']) && $_GET['method_name']) == 'get_city') {
     get_city();
}

次のようにコンテンツタイプを設定する必要もあります

header('Content-type: application/json');

最終的なコードは次のようになります

<?php
include 'json_config.php';
function get_city(){
mysql_query("SET NAMES utf8;");
mysql_query("SET CHARACTER_SET utf8;");
$return=array();
//$country_name=$_POST['country_name'];

// get all products from products table
$result = mysql_query("SELECT *FROM city") or die(mysql_error());

header('Content-type: application/json');

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["city"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        // Full texts   id  dead_name   masged_name     gender  salah       notes   date    city_name   hour    min 
        $region_subscribt = array();
        $region_subscribt["id"] = $row["id"];
        $region_subscribt["city_name"] = $row["city_name"];
        $region_subscribt["region_name"] = $row["notes"];
        $region_subscribt["notes"] = $row["country_name"];
    // push single product into final response array
    array_push($response["city"], $region_subscribt);
}


// echoing JSON response
echo json_encode($response,JSON_UNESCAPED_UNICODE);
} else {
// no products found
$response["fail"] = 0;


// echo no users JSON
echo json_encode($response,JSON_UNESCAPED_UNICODE);
}
}//end of the function
if(isset($_GET['method_name']) && $_GET['method_name']) == 'get_city') {
 get_city(); }
?>

PHP、MySQL、XML、および JSON を使用して基本的な Web サービスを作成する

于 2014-02-02T12:28:51.557 に答える