如何创建一个 StringRequest,它在 php 脚本中执行 POST 来初始化参数,并在获取返回的 JSON 时执行 GET

问题描述 投票:0回答:1

所以我有一个在 mysql 数据库中搜索数据的 php 脚本,但对于搜索我想输入参数。通过 POST 来自 android 应用程序的参数

<?php 
$parametre=$_POST['parametre'];
$ordre=$_POST['ordre'];
$donneecherchee=$_POST['donneerecherchee'];
$auteur=$_POST['auteur'];
$recenseur=$_POST['recenseur'];

//database constants
define('DB_HOST', 'xxx.xxx.xxx.xxx');
define('DB_USER', 'xxxx');
define('DB_PASS', 'xxxxxxxx');
define('DB_NAME', 'xxxxxxx');
// Connect to the MySQL database

//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}

if ($parametre != null && $ordre != null && $auteur != null && $donneecherchee != null) {
    // echo(" Les paramatres sont:".$parametre.",".$ordre.",".$auteur.",".$donneecherchee.",".$recenseur);
    if ($auteur == "Mes recensements") {
        $queryused = "SELECT id, nom, dateprise, cin, observation, recenseur, usedurl FROM tsena WHERE " . $parametre . " = '" . $donneecherchee . "' AND recenseur = '" . $recenseur . "' ORDER BY id " . $ordre.";";
        $executedquery = $conn->prepare($queryused);
        $executedquery->execute();

        $executedquery->bind_result($id, $nom, $dateprise, $cin, $observation, $recenseur, $usedurl);

        $tsena = array();

        // traversing through all the result
        while ($executedquery->fetch()) {
            $temp = array();
            $temp['id'] = $id;
            $temp['nom'] = $nom;
            $temp['dateprise'] = $dateprise;
            $temp['cin'] = $cin;
            $temp['observation'] = $observation;
            $temp['recenseur'] = $recenseur;
            $temp['usedurl'] = $usedurl;
            array_push($tsena, $temp);
        }
        // echo the result as JSON
        echo json_encode($tsena);
    } else {
        $queryused2 = "SELECT * FROM tsena WHERE " . $parametre . " = '" . $donneecherchee . "' ORDER BY id " . $ordre .";";
        $executedquery2 = $conn->prepare($queryused2);
        $executedquery2->execute();

        $executedquery2->bind_result($id, $nom, $dateprise, $cin, $observation, $recenseur, $usedurl);

        $tsena2 = array();

        // traversing through all the result
        while ($executedquery2->fetch()) {
            $temp = array();
            $temp['id'] = $id;
            $temp['nom'] = $nom;
            $temp['dateprise'] = $dateprise;
            $temp['cin'] = $cin;
            $temp['observation'] = $observation;
            $temp['recenseur'] = $recenseur;
            $temp['usedurl'] = $usedurl;
            array_push($tsena2, $temp);
        }

        // echo the result as JSON
        echo json_encode($tsena2);
    }
}else{
    $data = [
        "id" => 0,
        "nom" => "Aucun",
        "dateprise" => "Aucune date",
        "cin" => "Aucun CIN",
        "observation" => "Aucune observation",
        "recenseur" => "Aucun recenseur",
        "usedurl" => "Aucune image"
    ];
    // Output the JSON data
    echo json_encode([$data]);
}

?>

我想获取返回的 json 并将其显示在我的应用程序中,但这需要我使用 GET 创建另一个 StringRequest,这样做将创建一个带有空参数的脚本。

Android 部分如下:

`private void loadProducts(String parametre, String ordre, String donneerecherchee,String auteur,String recenseur) { StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_tsena, 新的 Response.Listener() { @覆盖 公共无效onResponse(字符串响应){ 尝试 { JSONArray 数组 = 新 JSONArray(响应); System.out.println("服务器响应:"+response);

                        //traversing through all the object
                        for (int i = 0; i < array.length(); i++) {

                            JSONObject product = array.getJSONObject(i);

                            //adding the product to product list
                            tsenaList.add(new Tsena(
                                    product.getInt("id"),
                                    product.getString("nom"),
                                    product.getString("cin"),
                                    product.getString("dateprise"),
                                    product.getString("observation"),
                                    product.getString("recenseur")
                            ));
                        }

                        //creating adapter object and setting it to recyclerview
                        TsenaAdapter adapter = new TsenaAdapter(ConsultTsena.this, tsenaList);
                        recyclerView.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(ConsultTsena.this,"Erreur de load:"+error,Toast.LENGTH_LONG).show();
                    Log.e("Volley Erreur", error.toString());
                }
            }){
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("parametre", parametre);
            params.put("ordre", ordre);
            params.put("donneerecherchee",donneerecherchee);
            params.put("auteur",auteur);
            return params;  // Set the parameters for the request
        }
    };

    //adding our stringrequest to queue
    Volley.newRequestQueue(this).add(stringRequest);
}`

我能做什么?

我测试过:

  • 查看参数是否传入
  • 如果查询不正确;

我的 logcat 中有一个空的 json [],这不应该是这样,因为我在 Powershell 中执行查询并返回了一些东西。

php android android-volley
1个回答
0
投票

抱歉打扰大家,我只是把参数设置为GET。我通过 android 部分的 url 拍摄它们。它确实有效,但不是很安全

© www.soinside.com 2019 - 2024. All rights reserved.