Simple Android JSON parsing example with output into ListActivity

One of my planned projects in the upcoming 2011 is the Magento – Android application. I already did a fair part on the Android side, plus I started the Magento Mapy extension few weeks ago. Due to my recent one month vacation I was “off the grid” when it comes to my free time work so all my stuff were in a “pause mode”.

Besides my playing around with Android and XML-RPC, here is a simple JSON parsing example that might get you started with more serious things.

In this example, I am connecting to Twitters public timeline JSON url.

package net.theweblogix.demo.andy1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class HomeActivity extends ListActivity {

    /** Called when the activity is first created. */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

		setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.fetchTwitterPublicTimeline()));
    }

	public ArrayList<String> fetchTwitterPublicTimeline()
    {
    	ArrayList<String> listItems = new ArrayList<String>();

    	try {
			URL twitter = new URL(
					"http://twitter.com/statuses/public_timeline.json");
			URLConnection tc = twitter.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(
					tc.getInputStream()));

			String line;
			while ((line = in.readLine()) != null) {
				JSONArray ja = new JSONArray(line);

				for (int i = 0; i < ja.length(); i++) {
					JSONObject jo = (JSONObject) ja.get(i);
					listItems.add(jo.getString("text"));
				}
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listItems;
    }
}

Please direct your attention on the listItems.add(jo.getString(“text”)); line. This is the part that I am grabbing a “text” attribute/property of single JSON object. To get a more “visual” picture of all available attributes/properties you might want to take a look at the XML version of twitters public timeline. This way you will get nice colored XML in your browser, where you can see all available attributes.

Cheers.

2 Responses to “Simple Android JSON parsing example with output into ListActivity”

  1. Andres Koong Says:

    This is certainly these types of an incredible contraption the fact that you can be offering also, you supply it all away meant for no cost. That i appreciate experiencing internet websites the fact that recognize any price for offering a good good quality reference meant for totally free. It’s any aged exactly what all-around will come all-around schedule ..


  2. Sylvia Cespedes Says:

    I’m and so blessed to own found out the following site. People essentially claimed all of us simply just what exactly When i opted to help pay attention to help in addition to afterward a lot of. Amazing publishing in addition to regards yet again intended for performing this particular not any fee!


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>