JSon Example In Android

This example explains how to handle JSon parser. It shows how to read and write data to and from a JSon file.


1.) Create a new project by File-> New -> Android Project name it JSonExample.

2.) Write following into main.xml file:

<!--?xml version="1.0" encoding="utf-8"?-->
    android:orientation="vertical" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/result" />

3.) Put INTERNET permission to manifest file:


4.) Run for output.


1.) Create a project named JSonExample and set the information as stated in the image.

Build Target: Android 4.0
Application Name: JSonExample
Package Name: com. example. JSonExample
Activity Name: JSonExampleActivity
Min SDK Version: 14

2.) Open JSonExampleActivity.java file and write following code there:

package com.example.jsonexample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;

public class JSonExampleActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
				new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        String readTwitterFeed = readTwitterFeed();
		try {
			JSONArray jsonArray = new JSONArray(readTwitterFeed);
					"Number of entries " + jsonArray.length());
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				Log.i(JSonExampleActivity.class.getName(), jsonObject.getString("text"));
		} catch (Exception e) {

	public String readTwitterFeed() {
		StringBuilder builder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null) {
			} else {
				Log.e(JSonExampleActivity.class.toString(), "Failed to download file");
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		return builder.toString();
   public void writeJSON() {
    	JSONObject object = new JSONObject();
    	try {
    		object.put("name", "EDUMobile");
    		object.put("score", new Integer(200));
    		object.put("current", new Double(152.32));
    		object.put("nickname", "Android Tutor");
    	} catch (JSONException e) {

3.) Compile and build the project.


About the Author

Leave a Reply 9 comments

krishna - July 26, 2012 Reply

hi ur blog is so good.

cv - December 8, 2012 Reply

Very good tutorial. The link from tutorial is not available anymore , so i tried your code with another link but i receive message ” Unfortunately JSonExampleActivity has stopped.
What cased this error?

hassan gillani - December 19, 2012 Reply

thanks alot excellent work ….keep it up :-)

gunaseelan - December 20, 2012 Reply

Can you give us a source code in android which reads a json file from the local disk and manipulate in a list view.?

gunaseelan - December 20, 2012 Reply

Please reply me

Tanaya - December 30, 2012 Reply

Hi i want to link android to mysql database thr netbeans, wil u please help me

trubi - February 12, 2013 Reply

What is the difference between algorithm and steps. If the steps are what you have to do, where do you create main.xml?

fdous - July 21, 2013 Reply

very nice blog. U are great!!

Bhadresh - October 12, 2013 Reply

I read your blog but there is one confusion that what is strict mode
new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());

Leave a Reply: