Si8
Si8

Reputation: 9225

How to put JSON information in an array to use it for displaying in ListView

I have an Activity which has three fragments and I am trying to retrieve a JSON file from my server which I will be updating on a daily basis.

My JSON file is located here: http://pagesbyz.com/test.json

Because there are fragments, I used the following code in my MainActivity class:

DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httppost = new HttpPost("http://pagesbyz.com/test.json");
        // Depends on your web service
        httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httppost);           
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();
            // json is UTF-8 by default
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            result = sb.toString();
            Toast.makeText(getApplicationContext(), result, 2000).show();
        } catch (Exception e) { 
            // Oops
        }
        finally {
            try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
        }

What I really need to do is retrieve the TYPE field in my JSON file and separate it into three tabs and put it inside a ListView like this:

enter image description here

I would like to know once I have read the JSON file by using the code on top how do I proceed... Thanks for the help :)

Should I query on each fragment and then look for the TYPE field? Would that be easier?

Curious as to why the Toast did not execute... Here is the pastebin for my main activity: http://pastebin.com/gKTCeH79

Upvotes: 1

Views: 733

Answers (1)

DejanRistic
DejanRistic

Reputation: 2039

What you want to do is use Androids JSONObject and JSONArray to access your json data.

For example since your root is a json array you want to instantiate a JSONArray object from your json data you have.

JSONArray jsonArray = new JSONArray(jsonString);

Now from this array you can grab individual JSONObject for each object in your array.

JSONObject objectOne = new JSONObject(0); // Grabs your first item

From the JSONObject you can now access your three values.

String type = objectOne.get("type") // Will give you the value for type

JSONArray: http://developer.android.com/reference/org/json/JSONArray.html

JSONObject: http://developer.android.com/reference/org/json/JSONObject.html

Another way is to use a framework that lets you deserialize json into Java POJO's (Plain Old Java Objects). Gson is the easiest to use.

The basic idea is you make an object that relates directly to your json objects, after you have this you can easily store your data in java objects and use it however you like.

GSON example:

Gson gson = new Gson();
MyCustomClass obj2 = gson.fromJson(json, MyCustomClass.class);

Where MyCustomClass would contain the variables id, type, and data.

GSON reference: https://sites.google.com/site/gson/gson-user-guide

Good Luck!

Upvotes: 1

Related Questions