【问题标题】:Android (libGDX) http connection with mySQL using PHP server side scripting使用 PHP 服务器端脚本与 mySQL 的 Android (libGDX) http 连接
【发布时间】:2017-04-05 18:39:14
【问题描述】:

我正在尝试通过服务器端 PHP 脚本将我的 libGDX(java) android 项目与 mySQL 数据库连接,以便使用 POST 方法(这包括用户名和密码)实现登录过程。

因此,我面临着意想不到的问题。为了您的信息,我在本地使用 XAMPP 和 APACHE Web 服务器。

我所面临的!有时 PHP 脚本会返回以下响应字符串,就好像无法识别 POST 参数一样(尽管 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 Studio 上)在按 2-5 次 btnclickLogin()(如下所示)后停止显示任何日志,它实现了登录活动。

在我看来,http 连接挂断了,可能单击按钮的侦听器不再响应!!!

更奇怪的是,有时相同的代码,返回“成功”,一切正常。

android代码是下一个

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");

}

PHP 脚本是

对于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); }?>

对于 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");   ?>

请帮忙解决这个问题,让http连接“稳定”!!

非常感谢。

【问题讨论】:

  • 你能用print_r($_SERVER)检查你在php脚本上$_SERVER得到的所有值吗

标签: java php mysql android-studio libgdx


【解决方案1】:

Mishra 对我最近的回复感到抱歉。

是的,实际上我已经放置了一个 print_r 来查看 PHP 脚本的值,发现两个参数都丢失了。

在互联网上进行了如此多的搜索并找到了替代解决方案后,我还没有发现为什么会发生这种情况。所以,我替换了 HttpRequestBuilder。

最后,为了有一个完整的工作代码,我使用了下一个,现在一切正常。

 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);
 final HttpRequest httpRequest = new HttpRequest(Net.HttpMethods.POST);
 httpRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
 httpRequest.setHeader("Upgrade", "HTTP/1.1, HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11");
 httpRequest.setUrl(LOGIN_URL);
 httpRequest.setContent(HttpParametersUtils.convertHttpParameters(parameters));
 httpRequest.setTimeOut(6000);

再次感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2016-07-06
    • 2018-03-30
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多