Search code examples
androidjsonandroid-ion

android parsing json data using ION


i worked on simple application that retrieve json data from server and display in my application. i am using this link to fetch data, json data

and my code is as below,

Ion.with(this)
            .load("http://marketplace.envato.com/api/edge/search:audiojungle,,happy.json")
            .asString().setCallback(new FutureCallback<String>() {

                @Override
                public void onCompleted(Exception arg0, String data) {
                    // TODO Auto-generated method stub

                    try {
                        JSONObject jObject = new JSONObject(data);
                        JSONArray jArray = jObject.getJSONArray("search");

                        for (int i = 0; i < jArray.length(); i++) {
                            JSONObject jObject_0 = jArray.getJSONObject(i);
                            JSONObject jObj = jObject_0
                                    .getJSONObject("item_info");
                            cost.setText(jObj.getString("cost"));
                            item.setText(jObj.getString("item"));
                            user.setText(jObj.getString("user"));
                            preview_value.setText(jObj
                                    .getString("preview_type"));
                            length.setText(jObj.getString("length"));

                            String s = jObj.getString("thumbnail");

                            Ion.with(getBaseContext()).load(s).withBitmap()
                                    .intoImageView(image);

                            Log.d("item", item.toString());

                        }

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });

i want to display multiple data according to list data. bt am getting every time same last data and set it into the components. so how to get it multiple values of same json object. please help me. thank you.


Solution

  • If you are retrieving Json from the server, then you should probably use Ion like so (using asJsonObject not asString):

        Ion.with(context)
                .load(url)
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                        // TODO
                    }
                });
    

    The rest is up to your parsing.

    If you want help with that, post your response's format and we could maybe help you parsing it.