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

如何使用android volley发布字符串并返回php web服务的json对象数组,php,android,arrays,json,android-volley,Php,Android,Arrays,Json,Android Volley,我试图发送一个字符串并返回对象的json数组,响应如下: // Creating volley request obj JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { @Override public void onResp

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


  ?>
//创建截击请求对象
JsonArrayRequest movieReq=新的JsonArrayRequest(url,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
Log.d(TAG,response.toString());
hidePDialog();
//解析json
对于(int i=0;i
在上面的代码中,fullname是变量,test是值,使用该变量,我试图在php变量中发送变量数据,并执行如下查询:

 // 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
    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脚本中接收测试数据,请使用此代码。确保您的请求方法类型

<?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 ";
    }

?>

那么,你能告诉我如何打印json响应(对象数组)吗?实际上php部分已经可以了。在java部分中,JsonArrayRequest不允许指定get或post.So。我在StringRequest(或object request)中更改了它。但现在的问题是它返回字符串,但我需要对象数组。请检查我的更新答案。您收到的字符串响应类型是什么?请分享它。如下:[{“id”:“442”,“全名”:“Pooja(18岁)”,“图片”:“uploadfinding\/uploads\/2017-02-0823:49:521486619389674.jpg”,“位置”:“lkn”,“描述”:null}]它是一个对象数组,但现在当使用stringRequest而不是JsonArrayRequest时,它作为整个字符串返回。您正在接收
JSONArray
,所以您只需使用
JSONArray
解析该字符串。