GSON


 

  1. Simple Parsing
  2. Serialization
  3. Deserialization
  4. Using TypeToken
  5. Using Typed Collection

 

Steps to do before:

  1. Download the latest version of Gson library ( jar file ) from here.
  2. For Eclipse: Copy the downloaded library file to “libs” directory of the project. Right click on jar file -> Build Path -> Add to Build path.
    For Android Studio: Copy the downloaded library file to “libs” directory of the project structure and add compile files(‘libs/your lib name here’) is in your build.gradle.
  3. you can also download from here.

 

1. Simple Parsing

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open src -> package -> MainActivity.java and add following code :

Step 4 : Open src -> package -> Create new class MyHttpURLConnection.java and add following code :

Step 5 : Open AndroidManifest.xml and add following code :

ScreenShot:

gson_simple gson_simple2

Download SourceCode

 

 

2. Serialization

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open src -> package -> MainActivity.java and add following code :

Step 4 : Open src -> package -> Create new class MyHelper.java and add following code :

Step 5 : Open AndroidManifest.xml and add following code :

ScreenShot:

gson_serialze

Download SourceCode

 

 

3. Deserialization

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open src -> package -> MainActivity.java and add following code :

Step 4 : Open src -> package -> Create new class MyHelper.java and add following code :

Step 5 : Open src -> package -> Create new class MyHttpURLConnection.java and add following code :

Step 6 : Open AndroidManifest.xml and add following code :

ScreenShot:

gson_deserialze gson_deserialze2

Download SourceCode

 

 

4. Using TypeToken

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open res -> layout -> Create new xml, list_item.xml and add following code :

Step 4 : Open src -> package -> MainActivity.java and add following code :

Step 5 : Open src -> package -> Create new class, HelperList.java and add following code :

Step 6 : Open src -> package -> Create new class .java and add following code :

Step 7 : Open AndroidManifest.xml and add following code :

ScreenShot:

gson_token gson_token3 gson_token2

Download SourceCode

 

5. Using Typed Collection

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open res -> layout -> Create new xml, list_item.xml and add following code :

Step 4 : Open src -> package -> MainActivity.java and add following code :

Step 5 : Open src -> package -> Create new class HelperList.java and add following code :

Step 6 : Open src -> package -> Create new class .java and add following code :

Step 7 : Open AndroidManifest.xml and add following code :

ScreenShot:

gson_collection gson_collection2

Download SourceCode