如何使用android-volley发布一个字符串并返回json数组的对象用于php web服务

时间:2022-10-22 22:34:26

I'm trying to send a string and returns json array of objects in response like like this:

我正在尝试发送一个字符串并返回响应的json数组,如下所示:

 // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {

                            try {
                                JSONObject obj = response.getJSONObject(i);
                                Movie movie = new Movie();
                                movie.setTitle(obj.optString("fullname"));
                                movie.setThumbnailUrl(obj.optString("image"));
                                movie.setRating(obj.optString("location"));

                                movie.setYear(obj.getInt("id"));


                                // adding movie to movies array
                                movieList.add(movie);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                       // adapter.notifyDataSetChanged();

                        adapter.reloadData();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        })

        {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("fullname", "test"); // fullname is variable and test is value.

                return params;
            }  };

In above code fullname is variable and test is value and using that variable I'm trying to send my variable data in my php variable and perform my query like this:

在上面的代码中,fullname是变量,test是值,并且使用该变量我正在尝试在我的php变量中发送我的变量数据并执行我的查询,如下所示:

<?php
    header("content-type:application/json");
    require_once("dbConnect.php");

    $fullname = $_REQUEST["fullname"];

    //echo $fullname."11";


    $sql = "SELECT id ,image,fullname,location from uploadfinding WHERE fullname like '%$fullname%'";

    $res = mysqli_query($conn,$sql);


    $result = array();

    while($row = mysqli_fetch_array($res)){
            array_push($result, array(

                "id"=>$row["id"],
                "fullname"=>$row["fullname"],
                "image"=>$row['image'],
                "location"=>$row["location"]));

                //echo " over";

        }


    $fp = fopen('results.json', 'w');
    fwrite($fp, json_encode($result));
    fclose($fp);            
    echo json_encode($result);

    mysqli_close($conn);


  ?>

But my value test not transfer in php variable $fullname. So problem is how to transfer value test to php variable $fullname.

但是我的值测试没有在php变量$ fullname中传输。所以问题是如何将值测试转移到php变量$ fullname。

1 个解决方案

#1


0  

If you want to receive test data the in PHP script then use this code. You make sure your request method type.

如果要在PHP脚本中接收测试数据,请使用此代码。您确保您的请求方法类型。

<?php
    header("content-type:application/json");
    require_once("dbConnect.php");

    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        $fullname = $_POST["fullname"]; 
        echo "Received fullname = ".$fullname;
    }
    else if($_SERVER['REQUEST_METHOD']=='GET')
    {
        $fullname = $_POST["fullname"]; 
        echo "Received fullname = ".$fullname;
    }

    $sql = "SELECT id ,image,fullname,location from uploadfinding WHERE fullname like '%$fullname%'";

    $res = mysqli_query($conn,$sql);

    if($res)
    {
        $result = array();
        $result["detial"] = array();

        while($row = mysqli_fetch_array($res)){

            // temporary array to create single category
            $tmp = array();
            $tmp["id"] = $row["id"];
            $tmp["fullname"] = $row["fullname"];
            $tmp["image"] = $row["image"];
            $tmp["location"] = $row["location"];
            $tmp["des"] = $row["ProductDescription"];

            // push category to final json array
            array_push($result["detial"], $tmp);
        }

        // keeping response header to json
        header('Content-Type: application/json');

        // echoing json result
        echo json_encode($result);
    }
    else
    {
        echo "No date found ";
    }

?>

For more detail please check this tutorial and read this and this documentation.

有关更多详细信息,请查看本教程并阅读本文档和本文档。

I hope this will help you and you get your answer.

我希望这会对你有帮助,你得到答案。

#1


0  

If you want to receive test data the in PHP script then use this code. You make sure your request method type.

如果要在PHP脚本中接收测试数据,请使用此代码。您确保您的请求方法类型。

<?php
    header("content-type:application/json");
    require_once("dbConnect.php");

    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        $fullname = $_POST["fullname"]; 
        echo "Received fullname = ".$fullname;
    }
    else if($_SERVER['REQUEST_METHOD']=='GET')
    {
        $fullname = $_POST["fullname"]; 
        echo "Received fullname = ".$fullname;
    }

    $sql = "SELECT id ,image,fullname,location from uploadfinding WHERE fullname like '%$fullname%'";

    $res = mysqli_query($conn,$sql);

    if($res)
    {
        $result = array();
        $result["detial"] = array();

        while($row = mysqli_fetch_array($res)){

            // temporary array to create single category
            $tmp = array();
            $tmp["id"] = $row["id"];
            $tmp["fullname"] = $row["fullname"];
            $tmp["image"] = $row["image"];
            $tmp["location"] = $row["location"];
            $tmp["des"] = $row["ProductDescription"];

            // push category to final json array
            array_push($result["detial"], $tmp);
        }

        // keeping response header to json
        header('Content-Type: application/json');

        // echoing json result
        echo json_encode($result);
    }
    else
    {
        echo "No date found ";
    }

?>

For more detail please check this tutorial and read this and this documentation.

有关更多详细信息,请查看本教程并阅读本文档和本文档。

I hope this will help you and you get your answer.

我希望这会对你有帮助,你得到答案。