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

Lê Khánh Vinh
  • 2,329
  • 4
  • 24
  • 63
  • Have you checked to see at which point the data disappears? (e.g. Does the `$post` variable contain the message being `POST`ed?) – summea Dec 02 '15 at 18:23
  • 1
    Thanks. Is there any way to check what data has been sent to server? How to check? – Lê Khánh Vinh Dec 03 '15 at 00: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 '
    ' . print_r($post, true) . '
    ';` after the `$post = json_decode(...);` line to see the current value of `$post`.
    – summea Dec 03 '15 at 00: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? – Lê Khánh Vinh Dec 03 '15 at 01:01
  • [Xdebug](http://xdebug.org/) might be something to try for heavier [debugging](http://php.net/manual/en/debugger-about.php), but for this case, have you tried checking the output of your PHP variables (like the previous comment suggests)? – summea Dec 03 '15 at 17:40
  • Can u explain in detail how to use Xdebug? I want to know what is the data has been sent and what is the respond from server. By the way i figure out. We need to look through the array at php $eventID and make querrry for every single value in the array – Lê Khánh Vinh Dec 03 '15 at 17:44
  • Unfortunately, explaining how to use Xdebug goes beyond the scope of the comment section. If you want to use [Xdebug](http://xdebug.org/), I would suggest looking through the [documentation](http://xdebug.org/docs/)! But have you tried just printing the output of your PHP variables in your PHP code, first? It can give you a fast indication of where your code isn't working as expected. :) – summea Dec 03 '15 at 17:56
  • Thanks. It would be easier to debug if we know the output of php file. I ll look through the doc. Can Xdebug help us see the data sent from app and the repond from the php file? – Lê Khánh Vinh Dec 03 '15 at 18:02
  • I think Xdebug will mostly focus on the PHP side... so it may not help you with the Android side. This is why I suggest checking the output of your PHP variables... so you can see if this is a PHP or Android issue. – summea Dec 03 '15 at 18:03
  • Can we check which data is sent using log? Anyway to implement? Im still blur about debugging app – Lê Khánh Vinh Dec 03 '15 at 18:07
  • Logs can be used, too, but if this isn't a production program, you can simply output PHP variable values using the method I described in Comment #3 above. – summea Dec 03 '15 at 18:12
  • 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 – Lê Khánh Vinh Dec 03 '15 at 18:15
  • Ah, I see what you mean... that's true. You'll probably want to print to the PHP error_log, then, in that case. So, instead of `echo` statements in your PHP, try using the `error_log()` function, like in [this example](http://stackoverflow.com/a/19487475/1167750). – summea Dec 03 '15 at 18:18
  • Many thanks for yrs help. So the we print to error log. The respond send back to android will not change? We can print any variable to the error log for debugging? – Lê Khánh Vinh Dec 03 '15 at 18:24
  • No problem! The response sent back to Android shouldn't be affected by the error log printing, because that output should only go to the error log itself (and not to the "standard" output). Printing to the error log should give you some hints about where the program is not working as expected. – summea Dec 03 '15 at 18:31
  • is this 2 line enable error log? ini_set("log_errors", true); ini_set("error_log", "/errorlog.log"); //send error log to log file specified here. Where should the log be and can we change to specific path? – Lê Khánh Vinh Dec 03 '15 at 18:34
  • [This other answer](http://stackoverflow.com/a/3531852/1167750) might help with setting up the error log. Also, for later, you might want to consider using [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) once you have this current issue working. – summea Dec 03 '15 at 18:36
  • One last thought for now: have you tried making the `WHERE EVENTID` part of your SQL query `WHERE EventID` to match the (assumed) case of your database column? [Case sensitivity depends on the database/environment](http://stackoverflow.com/a/153967/1167750) you are using, though, but this might save you some headaches later. – summea Dec 03 '15 at 19:25

0 Answers0