4 May 2012

JSON Parsing in android | JSON parsing android Example

Hello Friends,

In this tutorial we are going to learn how to parse JSON in android.
JSON is very light weight, structured, easy to parse and much human readable. JSON is best alternative to XML when your android app needs to interchange data with your server.


Here is the response from server in JSON format , Now let working on this parsing....

JSONParser.java

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class JSONParser 
{
 public JSONParser() 
 {}

 public String performGet_String(String url) 
 {
  String responce = null;
  try 
  {
   DefaultHttpClient client = new DefaultHttpClient();
   URI uri = new URI(url);
   HttpGet method = new HttpGet(uri);
   HttpResponse res = client.execute(method);
   InputStream data = res.getEntity().getContent();

   StringBuilder stringBuilder = new StringBuilder();
   int b;
   while ((b = data.read()) != -1)
   {
    stringBuilder.append((char) b);
   }
   responce = stringBuilder.toString();
   
  } catch (ClientProtocolException e) {
   
  } catch (IOException e) {   
   e.printStackTrace();
  } catch (URISyntaxException e) {
   
  }

  return responce;
 }
}

Main Activity.java

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.TargetApi;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends ListActivity 
{
 
 private static String url = "Your JSON URL here....";

 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  new ProgressTask(MainActivity.this).execute();
 }

 private class ProgressTask extends AsyncTask<String, Void, Boolean> 
 {
  private ProgressDialog dialog; 

  public ProgressTask(ListActivity activity) 
  {
   context = activity;
   dialog = new ProgressDialog(context);
  }

  private Context context;

  protected void onPreExecute() 
  {
   this.dialog.setMessage("Progress start");
   this.dialog.show();
  }

  @Override
  protected void onPostExecute(final Boolean success) 
  {
   if (dialog.isShowing()) 
   {
    dialog.dismiss();
   }
  }

  @TargetApi(Build.VERSION_CODES.KITKAT)
  protected Boolean doInBackground(final String... args) 
  {
   JSONParser jParser = new JSONParser();

   // get JSON data from URL
   String Responce = jParser.performGet_String(url);
   Log.i("Responce=", Responce);
   try 
   {
    JSONObject jsonObject=new JSONObject(Responce);
    JSONArray array=jsonObject.getJSONArray("QuestionList");
    Log.e("array size=", ""+array.length());
    
    
    for (int i = 0; i < array.length(); i++) 
    {
     JSONObject jsonObject2=array.getJSONObject(i);
     Log.i("question_id=", jsonObject2.optString("question_id"));
     
     JSONArray array2=jsonObject2.getJSONArray("AnswerText");     
     for (int j = 0; j < array2.length(); j++) 
     {
      JSONObject jsonObject3=array2.getJSONObject(j);
      Log.i("answer_text=", jsonObject3.optString("answer_text"));
     }
    }
   }
   catch (JSONException e) 
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   return null;
  }
 }
}

More Reference Link :

http://www.coderzheaven.com/2012/04/26/parsing-json-objects-in-android/
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/
http://androidexample.com/JSON_Parsing_-_Android_Example/index.php?view=article_discription&aid=71&aaid=95http://mrbool.com/how-to-use-json-to-parse-data-into-android-application/28944

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)

No comments:

Post a Comment