繁体   English   中英

如何使用android-volley发布字符串并为php Web服务返回json对象数组

[英]How to post a string using android-volley and returns json Array of objects for php web service

我正在尝试发送一个字符串并返回对象的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;
            }  };

在上面的代码中,全名是变量,测试是值,并且使用该变量,我试图在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);


  ?>

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

如果要接收测试数据,请使用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 ";
    }

?>

欲了解更多详情请查看教程和阅读这个这个文档。

希望这对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM