charilaos13 charilaos13 - 1 year ago 53
JSON Question

Receive JSON Output from PHP in Android

I am using this code in my PHP script:

echo json_encode($output);

and I get the following in my pages output:

[{"food_id":"1","food_description":"Free Range Egg","food_protein":"8"},{"food_id":"3","food_description":"Seeded Bread","food_protein":"6"}]

Can you please lead me the way on how I can receive the pages output in my Android Application and fit in in my object below? e.g.:


Answer Source

You need to do something like this..

JSONObject jsonObject = new JSONObject(response);// response is your json data which you are sending from php

JSONArray jsonArray = jsonObject.getJSONArray("mMessage");

for (int i = 0; i < jsonArray.length(); i++) {

    JSONObject jsonObj = jsonArray.getJSONObject(i);


and from php side you need to send data like this

public function get_data()
    $json_response = array();
    $sql_querry_arry = array(); // array filled with sql result
        $json_response['mMessage'] = $sql_querry_arry;

    echo json_encode($json_response);

Now receive output as a string and follow the steps i mention before

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download