PHP サービスを介してデータベース接続を使用する Android アプリケーションの開発を開始しました。
Apache 2.2 サーバーと php 5 を正常にインストールして構成しました。私のプロジェクトには接続の問題はありませんが、問題は次のとおりです。1)アプリケーションがphpサービスに接続し、クエリを実行して結果を取得します.2)しかし、いくつかのクエリを実行した後、サービスが応答しなくなり、その間に解決できませんでした. 2日。
サービスが応答しなくなったら、エクスプローラーから http:/localhost:8888/android_api/ (android_api は php サービス関数を含むフォルダー) と入力して確認すると、結果は応答なしとして表示されます。ページを更新すると、再び応答し始めます。
私のphpコード(localhost:8888/android_apiのindex.php):
<?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 変数には正しいデータが含まれています。どんな助けも神のようになります。