0

Hi I have send and arrayList eventIDBeacon using volley library. I want to ask how to decode the array and store in PhP array variable

Here is my code at android app

private void getEventDetailRespond(RequestQueue requestQueue) {
        eventDetail = new ArrayList<>();

        Map<String, Object> jsonParams = new ArrayMap<>();
        jsonParams.put(Config.EVENT_ID, eventIDBeacon);

        //Creating a JSONObject request
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,Config.DATA_URL, new JSONObject(jsonParams),
                new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject respond) {
                            try {
                                Toast.makeText(Beacon_MainActivity.this,"eventDetail respond "+respond.toString(),Toast.LENGTH_LONG).show();
                                eventArray = new JSONArray();

                                eventArray = respond.getJSONArray("result");
                                getEventDetail(eventArray);

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

                        }
                    },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(Beacon_MainActivity.this, "Unable to fetch data event Detail: " +error.getMessage(),Toast.LENGTH_LONG).show();
                    }
                });

        //Adding request to the queue
        requestQueue.add(jsonObjectRequest);
    }

Here is my PHP server but seem not to work. how to handle ArrayList at server side? im still stuck with this. I want to get back the array and use SQL query to query value in the array and return the result send back to the android app?

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

// decoding the json array
$post = json_decode(file_get_contents("php://input"), true);

$eventID = $post['EventID'];

require_once('dbconnect.php');

$sql = "SELECT EventID, EventTitle, EventDesc, EventTime FROM Event WHERE EVENTID = '$eventID'";

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

$result = array();


 while ($row = mysqli_fetch_array($res)){
        array_push($result,array(
            'EventID'=>$row['EventID'],
            'EventTitle'=>$row['EventTitle'],
            'EventDesc'=>$row['EventDesc'],
            'EventTime'=>$row['EventTime']
        ));
    }

    header('Content-Type: application/json');
    echo json_encode(array('result'=>$result), 256);    
    mysqli_close($con);
}

Any help should be much appreciate

18
  • Have you checked to see at which point the data disappears? (e.g. Does the $post variable contain the message being POSTed?) Commented Dec 2, 2015 at 18:23
  • 1
    Thanks. Is there any way to check what data has been sent to server? How to check? Commented Dec 3, 2015 at 0:17
  • It's not as complex as a debugger, but if you put echo or print_r() statements at different points in your PHP code (and then, perhaps, comment out your header() line for now...) it will print out what values you have at different points in time. For example, you could potentially add the line echo '<pre>' . print_r($post, true) . '</pre>'; after the $post = json_decode(...); line to see the current value of $post. Commented Dec 3, 2015 at 0:44
  • But i still know how to see the out put variable at php. Is there any software or browser can see what is output? Commented Dec 3, 2015 at 1:01
  • 1
    But the problem is that i have encoded the output to send back by echo json_encode(array('result'=>$result), if i insert echo then android can not decode respond object properly Commented Dec 3, 2015 at 18:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.