Step 1 : Simple JSON format :
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "umesh@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "suryawanshi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
/
/
/
/
so on...
Step 2 : Hello guys, this is comman JSONParser class. Use this code and parse any JSON. Just put this java file in your package.
package com.umesh.jsonparse;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String return jObj;
}
}
Step 3 : This is simple class which parse JSON and show values in listiview.
Main.java :
import java.net.URL;import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class Main extends ListActivity {
private static String url="http://api.androidhive.info/contacts/";
ArrayList<HashMap<String, String>> contactList;
JSONArray contacts=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
contactList=new ArrayList<HashMap<String,String>>();
JSONParser jparse=new JSONParser();
JSONObject jobject=jparse.getJSONFromUrl(url);
try
{
contacts=jobject.getJSONArray("contacts");
for(int i=0;i<contacts.length();i++)
{
HashMap<String,String> map=new HashMap<String, String>();
JSONObject c=contacts.getJSONObject(i);
map.put("id",c.getString("id"));
map.put("name",c.getString("name"));
map.put("email",c.getString("email"));
JSONObject phone=c.getJSONObject("phone");
map.put("phone",phone.getString("mobile"));
contactList.add(map);
}
}
catch(Exception e)
{
e.printStackTrace();
}
ListAdapter adapter=new SimpleAdapter(this,contactList,R.layout.listitems,new String[]{"name","email","phone"},new int []{R.id.tv_name,R.id.tv_email,R.id.tv_mobile});
setListAdapter(adapter);
}
}
Step 4 : As we are extending ListActivity, no need to use main layout with list view. Just define listitems. Following is the simple listitem layout
listitems.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="16dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_name"
android:text="email id"
android:textSize="14dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tv_mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_email"
android:text="Mobile : "
android:textSize="14dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_mobile"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
No comments:
Post a Comment