【发布时间】:2022-05-21 19:31:36
【问题描述】:
我试图通过 Volley 发出带有参数的 POST 请求。从我的 ANDROID APP 中的数据库响应查询结果完全正常,但是当我在 php 中显示此查询的值时,然后显示错误未定义索引变量“uploaded_by”。
JAVA 代码:
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERNAME,username);
params.put(KEY_PASSWORD,password);
params.put(KEY_EMAIL, email);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
-
标题
#
PHP 代码:
<?php
//This script is designed by Android-Examples.com
//Define your host here.
$servername = "localhost";
//Define your database username here.
$username = "root";
//Define your database password here.
$password = "";
//Define your database name here.
$dbname = "u288012116_and";
$connection = new mysqli($servername,$username,$password,$dbname) ;
// Innitialize Variable
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$StudentData = $_POST['uploaded_by'];
//fetch table rows from mysql db
$sql = "SELECT image_title,image_url FROM `imagelistviewtable` WHERE `uploaded_by` = '". $StudentData . "'";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$emparray = array();
while($row = mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
$json = json_encode($emparray);
if ($json === false) {
// Avoid echo of empty string (which is invalid JSON), and
// JSONify the error message instead:
$json = json_encode(array("jsonError", json_last_error_msg()));
if ($json === false) {
// This should not happen, but we go all the way now:
$json = '{"jsonError": "unknown"}';
}
// Set HTTP response status code to: 500 - Internal Server Error
http_response_code(500);
}
echo $json;
//close the db connection
mysqli_close($connection);
}
?>
【问题讨论】:
-
你不会在任何地方发送
uploaded_by_POST变量..你必须把它放在你的参数中 -
但我想根据用户在 android 应用程序的 edittext 中的输入数据在我的 php 文件中执行查询,使用 where 子句检查我的 php 代码我想从数据库接收查询信息并用 json 显示在 php 文件中格式
-
输入编辑文本的键是 upload_by 我在常量 KEY_USERNAME 中声明的
-
尝试使用
if($_SERVER['REQUEST_METHOD'] == 'POST') { print_r($_POST); return; }并分享结果 -
同样的错误没有在php文件中显示意味着没有从android应用程序接收php中的post请求参数,但在android应用程序中服务器响应正常,并且还显示有关查询的信息,但php文件有错误
标签: php android android-volley