【问题标题】:Android Volley - Post Request - Not Sending parameter to phpAndroid Volley - 发布请求 - 不向 php 发送参数
【发布时间】: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);
    }

  1. 标题

    #

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


【解决方案1】:
 public static void getServerData(final Context cntx) {
        RequestQueue queue = Volley.newRequestQueue(cntx);


        JsonObjectRequest jsObjRequest = new JsonObjectRequest(
                Request.Method.POST, "Url", json_object,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {

                         // Your response

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub

                    }

                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {

                // Your header parameter if you have otherwise this is optional

                return headers;
            }
        };
   queue.add(jsObjRequest);
}

【讨论】:

  • 我想在我的 php 文件中根据用户输入数据在 android 应用程序的 edittext 中使用 where 子句检查我的 php 代码我想从数据库接收查询信息并以 json 格式显示在 php 文件中
  • 主要问题是php变量没有从android应用程序接收值,所以它为什么不能在php文件中执行查询并从数据库中获取信息
【解决方案2】:

我回答这个问题有点晚了,但我认为它可能会对面临同样问题的人有所帮助。 我搜索了关于 SO 的所有类似问题,与 PHP 和 Volley api 有关,但我没有找到任何令人满意的答案。

问题是您将 Volley 库中的数据作为内容类型发送

应用程序/json

但您的 PHP 脚本需要 POST 数据作为内容类型

应用程序/x-www-form-urlencoded

在您的 PHP 脚本中,执行以下操作:

$_POST = json_decode(file_get_contents('php://input'), true);

if ( !empty($_POST['uploaded_by']) ) {

    $StudentData = $_POST['uploaded_by'];
    echo $StudentData;
}

现在如果你检查

if( isset($_POST['uploaded_by']) ){

    echo "something";
}

它现在应该可以工作了

【讨论】:

    猜你喜欢
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多