Tuesday 20 November 2012

JSON Parsing in Android - URL - 1

Demo:

1)main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="JSON Parsing"
        android:textColor="#ffffff"
        android:textSize="20dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvMatch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Match"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tvScore"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Score"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tvSummary"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Summary"
        android:textColor="#ffffff"
        android:textSize="20dp" />

</LinearLayout>


2)Activity:

package com.venky.jsonparsing1;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class JSONParsingActivity extends Activity {
      /** Called when the activity is first created. */
      TextView tvMatch, tvScore, tvSummary;

      String response;

      String URL = "http://json-cricket.appspot.com/score.json";

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.json_parsing);

            tvMatch = (TextView) findViewById(R.id.tvMatch);
            tvScore = (TextView) findViewById(R.id.tvScore);
            tvSummary = (TextView) findViewById(R.id.tvSummary);

            try {
                  response = new DefaultHttpClient().execute(new HttpGet(URL),
                              new BasicResponseHandler());

                  JSONObject jsonObject = new JSONObject(response);

                  String match = jsonObject.getString("match");
                  String score = jsonObject.getString("score");
                  String summary = jsonObject.getString("summary");

                  tvMatch.setText(match);
                  tvScore.setText(score);
                  tvSummary.setText(summary);

            } catch (ClientProtocolException 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();
            }
      }
}


3)Download this Project Click Here
  

No comments:

Post a Comment