I could really use some help here. I'm learning HttpRequest with libGDX, and I'm stuck with the setContent(); Here is the Java code I'm trying to run. And it works fine, except one little problem: It doesn't send the variables "name" and "score" to the httptest.php file. However the php file runs normaly, inserting empty values into the database. Eclipse even shows me the result about the success with an empty name and a 0 score:
我真的可以在这里使用一些帮助。我正在用libGDX学习HttpRequest,而且我坚持使用setContent();这是我正在尝试运行的Java代码。它工作正常,除了一个小问题:它不会将变量“name”和“score”发送到httptest.php文件。然而,php文件正常运行,将空值插入数据库。 Eclipse甚至向我展示了一个空名和0分的成功结果:
Status code : 200
Result : connected succesfully inserted and his score: 0
So it looks like the two programs communicate, except the java doesn't send the data to the php. I run out of ideas, and dont know what to do.
所以它看起来像两个程序通信,除了java不发送数据到PHP。我没有想法,也不知道该怎么做。
The java code:
java代码:
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("name", "testname1");
parameters.put("score", "56");
HttpRequest request = new HttpRequest(HttpMethods.POST);
request.setUrl("www.myurl.com/httptest.php");
request.setContent(HttpParametersUtils.convertHttpParameters(parameters));
// request.setContent("name=testname&score=10");
Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
@Override
public void handleHttpResponse(HttpResponse httpResponse) {
Gdx.app.log("Status code ", "" + httpResponse.getStatus().getStatusCode());
Gdx.app.log("Result ", httpResponse.getResultAsString());
}
@Override
public void failed(Throwable t) {
Gdx.app.log("Failed ", t.getMessage());
}
});
The php code:
php代码:
<?php
$name=$_POST["name"];
$scoreget=$_POST["score"];
$score=(int)$scoreget;
if (is_int ($score))
{
// Create connection
$con=mysqli_connect($mycon);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else{echo"connected ";}
$sql= "INSERT INTO scores (name, score, text) VALUES ('$name', '$score','')";
if (!mysqli_query($con, $sql))
{
die('Error: ' . mysqli_error($con));
}else{
echo "succesfully inserted ".$name." and his score: ".$score;
}
}else{
echo "Invalid numeric type";
}
?>
I also tested the httptest.php with this form and it works fine:
我也用这个表单测试了httptest.php,它工作正常:
<html>
<body>
<form action="httptest.php" method="post">
Name: <input type="text" name="name"><br>
Score: <input type="text" name="score"><br>
<input type="submit">
</form>
</body>
</html>
1 个解决方案
#1
0
Does it help if you correctly set the Content-Type header on the request?
如果您在请求中正确设置Content-Type标头,这会有帮助吗?
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
I'm wondering if PHP doesn't part the parameters in a POST body unless the correct content type is present.
我想知道除非存在正确的内容类型,否则PHP不会在POST主体中分配参数。
#1
0
Does it help if you correctly set the Content-Type header on the request?
如果您在请求中正确设置Content-Type标头,这会有帮助吗?
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
I'm wondering if PHP doesn't part the parameters in a POST body unless the correct content type is present.
我想知道除非存在正确的内容类型,否则PHP不会在POST主体中分配参数。