Wednesday 21 November 2012

JSON Parsing in Android - URL - 2

Demo:
 
1.Activity:

package com.venky.jsonparsing2;

import java.io.IOException;
import java.util.Vector;

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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class JSONParsing2Activity extends Activity {
      /** Called when the activity is first created. */

      String response;

      String Url = "http://api.androidhive.info/contacts/";

      static Vector<Contacts> vecContacts = new Vector<Contacts>();
      ListView lvList;
      ProgressDialog progressDialog;

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

            lvList = (ListView) findViewById(R.id.lvList);
            progressDialog = ProgressDialog.show(JSONParsing2Activity.this, null,
                        "Loading Please wait ...");
            new Thread() {
                  public void run() {
                        parsing();
                        runOnUiThread(new Runnable() {
                              public void run() {
                                    lvList.setAdapter(new MyAdapter());
                                    progressDialog.dismiss();
                              }
                        });
                  }
            }.start();

      }

      public void parsing() {
            JSONArray contactsArray = null;
            try {
                  response = new DefaultHttpClient().execute(new HttpGet(Url),
                              new BasicResponseHandler());

                  JSONObject jsonObject = new JSONObject(response);

                  contactsArray = jsonObject.getJSONArray("contacts");
                  for (int i = 0; i < contactsArray.length(); i++) {
                        Contacts contacts = new Contacts();
                        JSONObject jsonObject2 = contactsArray.getJSONObject(i);
                        contacts.id = jsonObject2.getString("id");
                        contacts.name = jsonObject2.getString("name");
                        vecContacts.add(contacts);
                  }
            } 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();
            }
      }

      public class MyAdapter extends BaseAdapter {

            @Override
            public int getCount() {
                  // TODO Auto-generated method stub
                  return vecContacts.size();

            }

            @Override
            public Object getItem(int position) {
                  // TODO Auto-generated method stub
                  return position;
            }

            @Override
            public long getItemId(int position) {
                  // TODO Auto-generated method stub
                  return position;
            }

            @Override
            public View getView(int position, View v, ViewGroup parent) {
                  Contacts contacts = vecContacts.get(position);
                  v = (LinearLayout) LayoutInflater.from(getApplicationContext())
                              .inflate(R.layout.style, null);

                  TextView tvid = (TextView) v.findViewById(R.id.tvId);
                  TextView tvname = (TextView) v.findViewById(R.id.tvName);

                  tvid.setText(contacts.id);
                  tvname.setText(contacts.name);
                  return v;
            }

      }
}

2.Contacts.java:

package com.venky.jsonparsing2;

public class Contacts {

      public String id = "";
      public String name = "";
}


3.main.xml file: 

<?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" >

    <ListView
        android:id="@+id/lvList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>



4.style.xml:

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

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

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

</LinearLayout>


5)Download this Project Click Here

No comments:

Post a Comment