0

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

 // 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:

<?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.

  • Please check my updated answer. – Farmer Feb 10 '17 at 06:12
  • What type of string response you received? please share it. – Farmer Feb 10 '17 at 07:20
  • like this:[{"id":"442","fullname":"Pooja(18 yr)","image":"uploadfinding\/uploads\/2017-02-0823:49:521486619389674.jpg","location":"lkn","Description":null}] –  Feb 10 '17 at 07:27
  • it is array of object but now it return as whole string when use stringRequest instead of JsonArrayRequest . –  Feb 10 '17 at 07:28
  • you are receiving `JSONArray` so you just parse that string by using `JSONArray`. – Farmer Feb 10 '17 at 08:04
  • I'm parsing but not work it ..See this http://stackoverflow.com/questions/42154299/how-to-handle-array-of-objects-in-response-while-using-stringrequest-in-android –  Feb 10 '17 at 08:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135358/discussion-between-shailesh-and-neo). – Farmer Feb 10 '17 at 08:31

1 Answers1

0

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

<?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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Farmer
  • 4,093
  • 3
  • 23
  • 47
  • then , can U tell me how print my json response(array of objects). –  Feb 10 '17 at 05:47
  • Actually php part is already ok. In java part JsonArrayRequest did not allow to specify get or post.So. I change it in StringRequest(or object request).but now to problem is it returns string but I want array of object. –  Feb 10 '17 at 07:07