Tuesday, 3 December 2013

JSON Parsing Example

About JSON

JSON stands for JavaScript Object Notation. It is a light-weight text-data interchange format. It is self - describing and easy to understand.

JSON uses JavaScript syntax for describing data objects, but it is still language and platform independent. JSON parsers and JSON libraries exists for many different programming languages


Example Code For JSON Parsing in Android:


activity_main.xml


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

</ScrollView>


MainActivity.java

package com.json.jsonparsingexample;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView1);
StrictMode.enableDefaults();
getData();
}

//Connection Creation to Server

public void getData()
{
String result = " ";
InputStream is = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("URL");
HttpResponse response = httpclient.execute(httpost);
if(response!=null)
{
System.out.println("Connection Created");
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
}
catch(Exception e)
{
Log.i("Error",e.toString());
tv.setText("Connection not Careated");
}
          //Converting Response to Stream
try
{
InputStreamReader isr = new InputStreamReader(is,"iso-8859-1");
BufferedReader reader = new BufferedReader(isr,8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine())!=null)
{
sb.append(line+"\n");
System.out.println(sb);
}
is.close();
result = sb.toString();
tv.setText(result);
}
catch(Exception ex)
{
Log.i("log_tag","Error converting result "+ex.toString());
tv.setText("Error converting result");
}
        //Parsing 


try{
String record = " ";
JSONArray jarray = new JSONArray(result);
for(int i=0;i<jarray.length();i++)
{
JSONObject jsonobj = jarray.getJSONObject(i);
record = record+"\n Name :"+jsonobj.getString("name")+"\n URL :"+jsonobj.getString("url");
tv.setText(record);
}
}
catch(JSONException exx)
{
Log.i("log_tag","Error parsing data "+exx.toString());
}
}

}



Manifest

Add internet permission in Manifest

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


That's it .....................

Enjoy Android App Development 

No comments:

Post a Comment