lundi 4 septembre 2017

How android accept multiple array data?

newbie here. I developed my backend with laravel, and it does return me result when i signed in to the system. But when this data pass to android, i am not sure how to handle it.

[
    {
        "id": 1,
        "name": "a",
        "email": "ddd@gmail.com",
        "address": null,
        "created_at": "2017-09-03 14:51:16",
        "updated_at": "2017-09-03 14:51:16",
        "items": [
            {
                "id": 1,
                "name": "test",
                "description": "test",                
                "pivot": {
                    "user_id": 1,
                    "items_id": 1,
                    "status": 1,
                    "created_at": "2017-09-03 14:51:48",
                    "quantity": 2,
                    "address": "gdhsjj",
                    "price": "2.0",
                    "updated_at": "2017-09-03 14:51:48"
                }
            },
            {
                "id": 1,
                "name": "test",
                "pivot": {
                    "user_id": 1,
                    "items_id": 1,
                    "status": 1,
                    "created_at": "2017-09-03 14:52:02",
                    "quantity": 1,
                    "address": "gdhsjj",
                    "price": "1.0",
                    "updated_at": "2017-09-03 14:52:02"
                }
            },
]
    },
    {
        "id": 2,
        "name": "dd",
        "email": "dd@gmail.com",
        "address": "123",
        "created_at": null,
        "updated_at": null,
        "items": []
    }
]

This is some example of my data when i receive from the server. I was able to display the data in android application. Here is the sample when i get the data, and how my server gonna to handle it.

 if (status.equals("SUCCESS")) {
                    JSONArray jsonArray = json.getJSONArray("data");
                    JSONArray jsonArrayDetail = json.getJSONArray("detail");
                    ArrayList<PurchasedInfo> purchasedInfos = new ArrayList<PurchasedInfo>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        JSONObject jsobObjectDetail = jsonArrayDetail.getJSONObject(i);
                        PurchasedInfo purchasedInfo = new PurchasedInfo();
                        purchasedInfo.setI_id(jsobObjectDetail.getString("id"));
                        purchasedInfo.setI_name(jsonObject.getString("name"));


    purchasedInfo.setI_price(jsonObject.getString("price"));
                            purchasedInfo.setI_image(jsonObject.getString("image"));
                            purchasedInfos.add(purchasedInfo);
                        }
}

This works fine if the data is pretty straight forward, but if i want to get the data from items, i am not sure how to do that. Does everything need to convert first from the server? Or i can straight display the data when i get the data?

ps:My question is a bit messy, hope you guys can understand or help me edit my question. Thanks. If anything unclear, please do let me know.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire