Quantcast
Channel: adblogcat.com » HTTP
Viewing all articles
Browse latest Browse all 2

Parse JSON data from a web server and display on ListView

$
0
0

This article describes how to create an HTTP connection from an android device to a web server, grab JSON data, parse and display it on a ListView. The ListView is custom thus allowing me to add other Views along the ListView. This was written using Android 2.1 APIs.



Open your manifest.xml and add the following permissions outside of the tag:

<uses-permission android:name="android.permission.INTERNET" />

Open the main.xml file and add a ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<ListView android:id="@android:id/list" android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
</LinearLayout>

Notice here I am using

android:id/list

for the ListView. Why? Most people just implement the ListActivity with the custom layout simple_list_item_1 or any of the layouts that come with android. The problem is, when people want to add over Views (Buttons, TextView, etc) they cannot because they cannot set their main.xml as the contentView, so here I have done it differently.

Now to the code. Create a class JSONParser that extends ListActivity. The first thing we will do is add an ArrayAdapter that uses android’s simple_list_item_1 and uses an internal method populate() to give it the ArrayList.

public class JSONParser extends ListActivity {
	
	/** Called when the activity is first created. */
	@SuppressWarnings("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.populate()));
	}

Now the JSON parsing part. Create the function populate(). Inside create an HttpURLConnection with the URL that contains the json objects.

	private ArrayList<String> populate() {
		ArrayList<String> items = new ArrayList<String>();

		try {
			URL url = new URL
			("http://SOMETHING.json");
			HttpURLConnection urlConnection = 
				(HttpURLConnection) url.openConnection();
			urlConnection.setRequestMethod("GET");
			urlConnection.connect();
                        // gets the server json data
			BufferedReader bufferedReader = 
				new BufferedReader(new InputStreamReader(
						urlConnection.getInputStream()));

Finally, inside the same function, create a JSONArray and for the length of each JSONArray, get the JSONObject add the items by the TAG (in this case: text).


			String next;
			while ((next = bufferedReader.readLine()) != null){
				JSONArray ja = new JSONArray(next);

				for (int i = 0; i < ja.length(); i++) {
					JSONObject jo = (JSONObject) ja.get(i);
					items.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 items;
	}
}

Want to learn more?

Check out this article on how to connect to a remove server MYSQL database using PHP and display the results in a ListView


Viewing all articles
Browse latest Browse all 2

Trending Articles