我正在尝试通过服务器端 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() (如下所示)(实现登录 Activity )后停止显示任何日志。
在我看来,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 来查看 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);
再次感谢您的帮助。
关于java - Android (libGDX) 使用 PHP 服务器端脚本与 mySQL 进行 http 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40735911/