POSTメソッドを使用してログインプロセスを実装するために、私はlibGDX(java)アンドロイドプロジェクトをmySQLデータベースに接続しようとしています。ユーザー名とパスワードが含まれます)。Android(libGDX)PHPサーバサイドスクリプトを使用したmySQLとのhttp接続
したがって、私は予期できない問題に直面しています。あなたの情報については、私はXAMPPとAPACHE Webサーバーをローカルで使用しています。
私が直面していること! POSTパラメータを認識していないかのようにいくつかの倍のPHPスクリプトは、(POSTメッセージは、それらが含まれており、値(文字列)が含まれているという事実にもかかわらず!)、次の応答文字列を送り返す:
<b>Notice</b>: Undefined index: username in <b>C:\xampp\htdocs\login\login.php</b> on line <b>5</b><br />
<br />
<b>Notice</b>: Undefined index: password in <b>C:\xampp\htdocs\login\login.php</b> on line <b>6</b><br />
いくつかの他、デバッガ(Androidスタジオで)デバッグログを表示できるようにすると、ログインアクティビティを実装するbtnclickLogin()(下記参照)を2〜5回押してからログを表示しなくなります。
http接続がハングアップして、クリックボタンのリスナーが応答しなくなることがあります。
より奇妙なものは、同じコードをしばらくすると「成功」を返し、すべて正常に動作します。
アンドロイドのコードは次の
PHPスクリプトが手を教えてくださいdbConnect.php
<?php
define('HOST',"localhost");
define('USER',"root");
define('PASS',"");
define('DB',"userlogging");
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Connection failed: ' . $conn->connect_error);
$con->set_charset("utf8"); ?>
についてはlogin.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
//Creating sql query
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
//importing dbConnect.php script
require_once('dbConnect.php');
//executing query
$result = mysqli_query($con,$sql);
//fetching result
$check = mysqli_fetch_array($result);
//if we got some result
if(isset($check)){
//displaying success
echo "success";
}else{
//displaying failure
echo "failure";
}
mysqli_close($con); }?>
について
ある
private void btnclickLogin() {
//Getting values from edit texts
Gdx.app.setLogLevel(Application.LOG_DEBUG);
final String username = usernamefld.getText().toString().trim();
final String password = passwordfld.getText().toString().trim();
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("username", username);
parameters.put("password", password);
Gdx.app.debug("Login process started.", "Username=/" + username + "/ Password=/" + password + "/");
HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
HttpRequest httpRequest;
httpRequest = requestBuilder.newRequest().method(Net.HttpMethods.POST).url("http://192.168.1.2/login/login.php").content(HttpParametersUtils.convertHttpParameters(parameters)).build();
httpRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpRequest.setTimeOut(6000);
Gdx.net.sendHttpRequest(httpRequest, new HttpResponseListener() {
@Override
public void handleHttpResponse(Net.HttpResponse httpResponse) {
String status = httpResponse.getResultAsString().trim();
Gdx.app.debug("Return result by the server=", status);
if(status.contains("success"))
game.setScreen(new StartingScreen(game));
}
@Override
public void failed(Throwable t) {
String status = "failed";
Gdx.app.debug("Connection failed due to the next error:", t.getMessage());
}
@Override
public void cancelled() {
}
});
httpRequest.reset();
Gdx.app.debug("Exiting", "From login button function");
}
ですそれを克服するHTTP接続を "安定"にしてください!
ありがとうございます。
'print_r($ _ SERVER)'を使って、あなたのPHPスクリプトで '$ _SERVER'に入っているすべての値をチェックできますか? –