Search This Blog

Monday 23 April 2012

how to take screen shot from emulater for android project


step 1:Clock on window menu from Eclipse -->Show view-> Device-- You can see a camera icon
step 2:After the runing your activity lake a shot with comera icon and save it

API key generation xp/windows7 for android app

Open a command prompt and navigate to a file called debug.keystore. The location of this file depends on what OS you are using. I use Windows XP and my debug.keystore file is located at C:\Documents and Settings\User\.android.
As a side note, before the Android 1.5 SDK came out, my debug.keystore file was located in a different location (C:\Documents and Settings\HP_Administrator\Local Settings\Application Data\Android). When I later updated to 1.5 I ran into all sorts of problems getting the maps to work.
if you use a Mac or Linux OS, I've discovered that you'll find the debug.keystore file at: ~/.android/.

The debug.keystore contains a digitial certificate that android uses to launch applications on the emulator. The certificate is called 'androiddebugkey'.  We'll need to get some an MD5 hash for this certificate in order register for our map api key.

Hopefully you've located the debug.keystore file, so now we'll use Java's keytool utility to get the MD5.

I use XP so I navigated to the proper directory by opening a command prompt and typing:
cd C:\Documents and Settings\HP_Administrator\.android

Once you've navigated to the proper directory for your os, type this in...
keytool -list -alias androiddebugkey -storepass android -keypass android -key
store debug.keystore


Once you hit the enter key, you'll see something like this (the actual MD5 that we're interested in is the last line):
androiddebugkey, Mar 10, 2009, PrivateKeyEntry,
Certificate fingerprint (MD5): D1:16:4A:BD:73:73:A4:56:9D:CD:9A:44:A2:6C:11:AC


Now open your browser and go to the following url...
http://code.google.com/intl/ja/android/maps-api-signup.html

Accept the agreement and paste the md5 that the key tool returned to you, in my case it was something like...
D1:16:4A:BD:73:73:A4:56:9D:CD:9A:44:A2:6C:11:AC

Then press "Generate API Key" and you'll be redirected to a page that has your api key.

Now
that you have the api key, you can use it in the main.xml layout file
of your MapTest application. Here's what mine looks like:

<?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">
    <com.google.android.maps.MapView
        android:id="@+id/mapview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="0q7NUYm4bgzeXlqXtKYVPJDRWUJmt8Cu0gvbWMx"
        />
</LinearLayout>


You'll also have to add some entries to your manifest file, here's what mine looks like...

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.remwebdevelopment.maptest"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <!-- this was the kicker - had to import the library com.google.android.maps!!! -->
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".MapTest"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <!--make sure you add the following permissions to your app!!!-->
    <uses-permission
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="android.permission.INTERNET">
    </uses-permission>
    <uses-permission
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="android.permission.ACCESS_FINE_LOCATION">
    </uses-permission>
</manifest>


Here's what my MapActivity looks like(note that it extends MapActivity, not Activiy)...

package com.remwebdevelopment.maptest;

import com.google.android.maps.MapActivity;


import android.os.Bundle;

public class MapTest extends MapActivity {

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

        //you could get a reference to the map view from the main.xml
        //layout like this...
        //mMapView = (MapView)findViewById(R.id.mapview1);
    }

    //you must provide an implementation for isRouteDisplayed()
    //when you extend MapActivity...
    @Override
    protected boolean isRouteDisplayed() {
    return false;
    }
}

You should see a map of the world when you run it.
Hopefully this will get you up and running.
___________________________________________________________________________
--------------- For Windows 7 below shows that api key genarate in command prompt some difference from windows XP.. see


C:\Program Files\Java\jre7\bin>keytool.exe -list -v -alias androiddebugkey -keys
tore "C:\Users\Student\.android\debug.keystore" -storepass android -keypass andr
oid
Alias name: androiddebugkey
Creation date: ?? ??????, ????
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[?]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 17ddf75
Valid from: Tue Apr 17 14:01:44 IST 2012 until: Thu Apr 10 14:01:44 IST 2042
Certificate fingerprints:
         MD5:  27:CB:33:C0:89:44:60:BD:82:25:D0:C6:A0:ED:56:E0
         SHA1: 6C:D4:C8:FC:A8:46:D1:3F:1C:C8:01:65:65:6F:6C:66:FE:9A:9A:11
         SHA256: 8C:43:A0:7C:E3:3A:DD:A7:66:1E:53:12:0C:E2:77:60:4E:3E:84:C5:D6:
CF:CB:C0:5A:0A:4A:B4:62:2E:2B:DC
         Signature algorithm name: SHA256withRSA
         Version: ?

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: D3 1B 80 EC 41 1F 77 BA   CB 1F C1 46 36 84 1D DB  ....A.w....F6...
0010: 41 90 9B D6                                        A...
]
]


C:\Program Files\Java\jre7\bin>

set Map.xml as below
<com.google.android.maps.MapView
                 android:id="@+id/mapview1"
                 xmlns:android="http://schemas.android.com/apk/res/android"
                
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:enabled="true"
                 android:clickable="true"
                
                    android:apiKey="0nzYV_RaZTPJJhNG95A29exaNtxyCxKr5Y_EC3A"

                 />
  I have other code samples
that might be helpful, I'll be posting them soon.
----------------------------------------------------------------------------------------------------------------
Obtain certificate’s fingerprint in Windows 7
  1. First go to C:\Users\username\.android directory there is file called debug.keystore we are going to use this file to generate certificate’s fingerprint.
  2. Open command prompt and enter following command on that
    keytool.exe -list -alias androiddebugkey -keystore "C:\Users\yourusername\.android\debug.keystore" 
    -storepass android -keypass android
Obtain certificate’s fingerprint in Mac OSX or Linux
  1. In Linux or Mac OSX debug.keystore is in ~/.android directory
  2. Open terminal and enter following command on that
    keytool -list -keystore ~/.android/debug.keystore
Then it will give certificate fingerprint like following
12:50:7F:C0:10:F0:A7:24:13:49:BF:42:76:76:38:83
After all go to the following site for obtaining Map API key
http://code.google.com/android/maps-api-signup.html
Follow the steps given bellow for obtain Map API key
  1. Then you can read and accept terms and conditions
  2. After that enter your certificate fingerprint in to given boxmapkey2
  3. Then click on Genarate API Key button
  4. Finally it will generate your keymapkey
Those are the steps that you have to follow for the obtain API key. You should copy this key and save  for future use. This key can be use in any kind of Android Google Map applications.

Tuesday 10 April 2012

JSON (JavaScript Object Notation) +Android


JSON Parsing


JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
JSON is built on two structures:
  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.
In JSON, they take on these forms:
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language.



What is JSON?

JSON stands for "JavaScript Object Notation" and is pronounced "Jason" (like in the Friday the 13th movies). It's meant to be a human-readable and compact solution to represent a complex data structure and facilitate data-interchange between systems.

Why use JSON?

There are tons of reasons why you would want to use JSON:
  • It's human readable... if it's properly formatted :-P
  • It's compact because it doesn't use a full markup structure, unlike XML
  • It's easy to parse, especially in JavaScript
  • A gazillion JSON libraries are available for most programming languagues
  • The data structure is easy to understand even if you're an idiot

The JSON format

There are just a few rules that you need to remember:
  • Objects are encapsulated within opening and closing brackets { }
  • An empty object can be represented by { }
  • Arrays are encapsulated within opening and closing square brackets [ ]
  • An empty array can be represented by [ ]
  • A member is represented by a key-value pair
  • The key of a member should be contained in double quotes. (JavaScript does not require this. JavaScript and some parsers will tolerate single-quotes)
  • Each member should have a unique key within an object structure
  • The value of a member must be contained in double quotes if it's a string (JavaScript and some parsers will tolerates single-quotes)
  • Boolean values are represented using the true or false literals in lower case
  • Number values are represented using double-precision floating-point format. Scientific notation is supported
  • Numbers should not have leading zeroes
  • "Offensive"" characters in a string need to be escaped using the backslash character
  • Null values are represented by the null literal in lower case
  • Other object types, such as dates, are not properly supported and should be converted to strings. It becomes the responsability of the parser/client to manage this.
  • Each member of an object or each array value must be followed by a comma if it's not the last one
  • The common extension for json files is '.json'
  • The mime type for json files is 'application/json'
Example:
{
   "anObject": {
      "numericProperty": -122,
      "stringProperty": "An offensive \" is problematic",
      "nullProperty": null,
      "booleanProperty": true,
      "dateProperty": "2011-09-23"
   },
   "arrayOfObjects": [
      {
         "item": 1
      },
      {
         "item": 2
      },
      {
         "item": 3
      }
   ],
   "arrayOfIntegers": [
      1,
      2,
      3,
      4,
      5
   ]
}

JSON in JavaScript

Because JSON derives from JavaScript, you can parse a JSON string simply by invoking the eval() function. The JSON string needs to be wrapped by parenthesis, else it will not work! This is the #1 problem when programmers first start to manipulate JSON strings.
Example: <script type="text/language">

   // A valid json string
   var someJsonString = '{"someProperty":"someValue"}';

   // jsonObject will contain a valid JavaScript object
   var jsonObject = eval('(' + someJsonString + ')');

   // Will display the string 'someValue'
   alert(jsonObject.someProperty);

</script>

You can create JavaScript objects using the JSON syntax directly in your code.
Example: <script type="text/language">

   // jsonObject is a valid JavaScript object that can be used on the fly
   var jsonObject = { someProperty : "someValue" };

   // Will display the string 'someValue'
   alert(jsonObject.someProperty);

</script>

JSON Parsing in android

Let’s look at how to parse JSON objects in android
1> First we’ll need an example :
Lets look at a standard example from the json site http://json.org/example.html
     {"menu": {
                                  "id": "file",
                                  "value": "File",
                                  "popup": {
                                    "menuitem": [
                                      {"value": "New", "onclick": "CreateNewDoc()"},
                                      {"value": "Open", "onclick": "OpenDoc()"},
                                      {"value": "Close", "onclick": "CloseDoc()"}
                                    ]
                                  }
                                }}
you could either save this in a file or save it in a string…..like I’ve done
2> Android already contains the required JSON libraries
Lets create a JSON Object;
private JSONObject jObject;
and lets our example be a String ,
private String jString = "{\"menu\":    {\"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": 
 [ {\"value\": \"New\", \"onclick\":
 \"CreateNewDoc()\"}, 
 {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, 
 {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";
now we have to convert jString to the jObject ,
jObject = new JSONObject(jString); 
Now we have to start extracting the content from jObject ,
Lets extract the menu object by creating a new menu object,
JSONObject menuObject = jObject.getJSONObject("menu");
Now lets extract its atrtibutes ,
String attributeId = menuObject.getString("id");
String attributeValue = menuObject.getString("value");
JSONObject popupObject = menuObject.getJSONObject("popup");
since “popup” is not plainly a String lets extract it to an object ,
3> Now popup contains an array of “menuitem”
So, we’ll have to extract it to a JSONArray,
 JSONArray menuitemArray = popupObject.getJSONArray("menuitem"); 
Since it contains 3 items lets put it in a for loop.
for (int i = 0; i < 3; i++) {
                        System.out.println(menuitemArray.getJSONObject(i)
                                        .getString("value").toString());
                        System.out.println(menuitemArray.getJSONObject(i).getString(
                                        "onclick").toString());
                }
Basically thats it , u should see the output in the DDMS
4> The full code is as below,
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;

public class JsonParser extends Activity {
        private JSONObject jObject;
        private String jString = "{\"menu\": {\"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\":
 [ {\"value\": \"New\", 
 \"onclick\": 
\"CreateNewDoc()\"},
 {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                try {
                        parse();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
        private void parse() throws Exception {
                jObject = new JSONObject(jString);

                JSONObject menuObject = jObject.getJSONObject("menu");
                String attributeId = menuObject.getString("id");
                System.out.println(attributeId);

                String attributeValue = menuObject.getString("value");
                System.out.println(attributeValue);

                JSONObject popupObject = menuObject.getJSONObject("popup");
                JSONArray menuitemArray = popupObject.getJSONArray("menuitem");

                for (int i = 0; i < 3; i++) {
                        System.out.println(menuitemArray.getJSONObject(i)
                                        .getString("value").toString());
                        System.out.println(menuitemArray.getJSONObject(i).getString(
                                        "onclick").toString());
                }
        }
}




Problem: How to parse JSON response? 
Solution:
Many times we get JSON string as a response from web server, now we have to parse the JSON string into either array, objects or values to display the response in the particular views like ListView, TextView, etc.
We can use JSONObject and JSONArray for parsing the response string.

Now, consider this sample JSON response string for the understanding of parsing example:

view plaincopy to clipboardprint?
  1. {"FirstObject": { "attr1":"one value" ,"attr2":"two value",  
  2.   
  3.    "sub": { "sub1":[ {"sub1_attr":"sub1_attr_value" },{"sub1_attr":"sub2_attr_value" }]}  
  4.   }  
  5. }
And it looks as below in FireBug tool:

Android JSON Parsing example
Now, let me start to give step by step demo for parsing the same JSON resoponse:
Step – 1:
create a JSONObject with the received response string:
view plaincopy to clipboardprint?
  1. JSONObject jsonObject = new JSONObject(strJSONResponse);
Step – 2:
Get the main object from the created json object by using getJSONObject() method:
view plaincopy to clipboardprint?
  1. JSONObject object = jsonObject.getJSONObject("FirstObject");
Step – 3:
Now this FirstObject contains 2 strings namely “attr1″,”attr2″ and a object namely “sub”.
So get 2 strings by using getString() method.
view plaincopy to clipboardprint?
  1. String attr1 = object.getString("attr1");  
  2. String attr2 = object.getString("attr2");
and get a sub object by using the same getJSONObject() method as we have used above:
JSONObject subObject = object.getJSONObject("sub");
Step – 4:
Now this “sub” sub-object contains 1 array namely “sub1″. So we can get this JSON array by using
getJSONArray() method:
view plaincopy to clipboardprint?
  1. JSONArray subArray = subObject.getJSONArray("sub1");
Now, we just need to process this array same as simple string array:
view plaincopy to clipboardprint?
  1. for(int i=0; i<subArray.length(); i++)  
  2.     {  
  3.         strParsedValue+="\n"+subArray.getJSONObject(i).getString("sub1_attr").toString();  
  4.     }
This is complete now. Enjoy
Output Snapshot:

Android - JSON Parsing tutorial
Full source code:
view plaincopy to clipboardprint?
    package com.technotalkative.jsonparsing;  
      
    import org.json.JSONArray;  
  1. mport org.json.JSONException;  
  2. import org.json.JSONObject;  
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.   
  8. /** 
  9.  * 
  10.  * Demo of JSON Parsing 
  11.  * @author Paresh N. Mayani 
  12.  * http://www.technotalkative.com/android-json-parsing/ 
  13.  * 
  14.  */  
  15. public class JSONParsingActivity extends Activity {  
  16.     /** Called when the activity is first created. */  
  17.   
  18.     TextView txtViewParsedValue;  
  19.     private JSONObject jsonObject;  
  20.   
  21.     String strParsedValue = null;  
  22.   
  23.     private String strJSONValue = "{\"FirstObject\":{\"attr1\":\"one value\" ,\"attr2\":\"two value\","  
  24.             +"\"sub\": { \"sub1\":[ {\"sub1_attr\":\"sub1_attr_value\" },{\"sub1_attr\":\"sub2_attr_value\" }]}}}";  
  25.   
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.   
  31.         txtViewParsedValue = (TextView) findViewById(R.id.textView1);  
  32.   
  33.         try {  
  34.             parseJSON();  
  35.   
  36.         } catch (JSONException e) {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41.   
  42.     public void parseJSON() throws JSONException  
  43.     {  
  44.         jsonObject = new JSONObject(strJSONValue);  
  45.   
  46.         //parsing Object value  
  47.         JSONObject object = jsonObject.getJSONObject("FirstObject");  
  48.         String attr1 = object.getString("attr1");  
  49.         String attr2 = object.getString("attr2");  
  50.   
  51.         strParsedValue="Attribute 1 value => "+attr1;  
  52.         strParsedValue+="\n Attribute 2 value => "+attr2;  
  53.   
  54.         //parsing sub-object  
  55.         JSONObject subObject = object.getJSONObject("sub");  
  56.   
  57.         // parsing array values  
  58.         JSONArray subArray = subObject.getJSONArray("sub1");  
  59.   
  60.         strParsedValue+="\n Array Length => "+subArray.length();  
  61.   
  62.         for(int i=0; i<subArray.length(); i++)  
  63.         {  
  64.             strParsedValue+="\n"+subArray.getJSONObject(i).getString("sub1_attr").toString();  
  65.         }  
  66.   
  67.         txtViewParsedValue.setText(strParsedValue);  
  68.     }  
  69. }  
  70.   
  71. // Actual JSON Value  
  72. /* 
  73. {"FirstObject": { "attr1":"one value" ,"attr2":"two value", 

  74.    "sub": { "sub1":[ {"sub1_attr":"sub1_attr_value" },{"sub1_attr":"sub2_attr_value" }]} 
  75.   } 
  76. }; */  
  77.   
  78. // Same JSON value in XML  
  79. /* 
  80. <FirstObject obj1="Object 1 value" obj2="Object 2 value"> 
  81.     <sub> 
  82.         <sub1 sub1_attr="sub1_attr_value" /> 
  83.         <sub1 sub1_attr="sub2_attr_value" /> 
  84.     </sub> 
  85. </FirstObject> */

Example Program


SingleMenuItemActivity.java

package com.androidhive.jsonparsing;

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

public class SingleMenuItemActivity extends Activity {
// JSON node keys
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHONE_MOBILE = "mobile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String name = in.getStringExtra(TAG_NAME);
String cost = in.getStringExtra(TAG_EMAIL);
String description = in.getStringExtra(TAG_PHONE_MOBILE);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.email_label);
TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
lblName.setText(name);
lblCost.setText(cost);
lblDesc.setText(description);
}
}



JSONParser.java



package com.androidhive.jsonparsing;

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;

}
}



AndroidJSONParsingActivity


package com.androidhive.jsonparsing;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class AndroidJSONParsingActivity extends ListActivity {

// url to make request
private static String url = "http://api.androidhive.info/contacts/";
// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";

// contacts JSONArray
JSONArray contacts = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

// Creating JSON Parser instance
JSONParser jParser = new JSONParser();

// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);

try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone number is agin JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_EMAIL, email);
map.put(TAG_PHONE_MOBILE, mobile);

// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
R.id.name, R.id.email, R.id.mobile });

setListAdapter(adapter);

// selecting single ListView item
ListView lv = getListView();

// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_EMAIL, cost);
in.putExtra(TAG_PHONE_MOBILE, description);
startActivity(in);

}
});

}

}

list_item.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="wrap_content"
android:orientation="horizontal">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="6dip"
android:paddingBottom="2dip" />
<!-- Description label -->
<TextView
android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
<!-- Linear layout for cost and price Cost: Rs.100 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Cost Label -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:gravity="left"
android:textStyle="bold"
android:text="Mobile: " >
</TextView>
<!-- Price Label -->
<TextView
android:id="@+id/mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:textStyle="bold"
android:gravity="left">
</TextView>
</LinearLayout>
</LinearLayout>

</LinearLayout>


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:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

</LinearLayout>


singe_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Name Label -->
<TextView android:id="@+id/name_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textColor="#43bd00"/>
<!-- Description Label -->
<TextView android:id="@+id/email_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"/>
<!-- Price Label -->
<TextView android:id="@+id/mobile_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
</LinearLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.jsonparsing"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AndroidJSONParsingActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Single List Item View -->
<activity
android:label="Single Menu Item"
android:name=".SingleMenuItemActivity" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />

</manifest>




http:/json.org



http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/