Android JSON Parsing Using Google gson-Part III

In the previous part of the Json series , we seen the complete basics of json.In this part we are going to actually parse a sample Json Object Using Google GSON.The Json will be not  passed as a String within the code , instead we will call the json object through  external URL through HTTP call.

Check for previous part :
Android Json Parsing basics tutorialAndroid Parsing Json tutorial part -2
Step 1:
Here is our Json Code that we are going to parse Using Gson in Android:

   "Os_version":[ {    "title":"Android 4.3 Jelly Bean (API level 18)"
         "title":"Android 4.4 KitKat (API level 19)"
         "title":"Android 5.0 Lollipop (API level 21)"
   "author":"Venkatesh Pillai",
   "price":"Open Source",
   "subject":"Last Android Os release"

Step 2: 
The next step is to download the Gson jar file.

Step 3:
Right-click the project's top most folder and select PROPERTIES ->JAVA BUILD PATH and select the libraries tab.

 Click the Add JARs button and select the Gson jar we added to the /libs folder 

Step 4:
We need to create one model class and and annotate field names  with the SerializedName annotation. When we add serialized name to field must match with key name from JSON

import java.util.ArrayList;
public class BeanSubject {

    private String subject; 
private String price;    
private String author;    
private ArrayList<BeanOs_version> beanOs_version;

    public BeanSubject(ArrayList<BeanOs_version> beanOs_version, String subject, String price, String author)
{        this.beanOs_version = beanOs_version;      
this.subject = subject;        
this.price = price; = author;    


public String getSubject() { 
return subject;   
public void setSubject(String subject) { 
this.subject = subject;    
public String getPrice() { 
return price;   
public void setPrice(String price) {  
this.price = price;    
public String getAuthor() {  
return author;    
public void setAuthor(String author) { = author;    
public ArrayList<BeanOs_version> getBeanOs_version() { 
return beanOs_version;    
public void setBeanOs_version(ArrayList<BeanOs_version> beanOs_version) {
this.beanOs_version = beanOs_version;    


Step 5:

public class BeanOs_version {

private String title;

    public BeanOs_version(String title)
this.title = title;    

    public String getTitle() {
return title;    
public void setTitle(String title) 
this.title = title;    

Step 6:
Here come the important task of creating HTTP request to the url to fetch the required data through Json. 
Create class to perform an HTTP request and retrieve the resource as a stream.

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class API 
private static Reader reader=null;                
public static Reader getData(String SERVER_URL) 
                        try {                                              
DefaultHttpClient httpClient = new DefaultHttpClient();                                                
HttpPost httpPost = new HttpPost(SERVER_URL);                                                
HttpResponse response = httpClient.execute(httpPost);                                                
StatusLine statusLine = response.getStatusLine();                                                
if (statusLine.getStatusCode() == 200) {                                                               
 HttpEntity entity = response.getEntity();                                                               
 InputStream content = entity.getContent();                                                                
reader = new InputStreamReader(content);                                                
} else {  
} catch (ClientProtocolException e) {                                           
} catch (IllegalStateException e) {                                             
} catch (IOException e) {                                                
return reader;                

Step 7:
Now change the code in Activity class and 
create GSON instance get data in required way that can be shown in your activity_main.xml layout file .
We will use Asyn Task Since the required details need to be fetched in background.Its not the good way to fetch data in tha UI thread. The doInBackground() method will help to work in background. 


public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {

new AsyncTask<Void,Void,Void>(){


protected void onPreExecute() {


progressDialog=new ProgressDialog(MyActivity.this);



} @Override

protected Void doInBackground(Void... voids) {

Reader reader=API.getData("....url....");//enter the json url 

beanSubject = new GsonBuilder().create().fromJson(reader, BeanSubject.class);

//Printing in Log File

Log.e("Subject: ", beanSubject.getSubject()+"");

Log.e("Author: ", beanSubject.getAuthor()+"");

Log.e("Price: ", beanSubject.getPrice()+"");


Os_versionList=new StringBuffer();

for(BeanOs_version Os_version: Os_versionArrayList){

Log.e("Os_version title: ",Os_version.getTitle()+"");

Os_versionList.append("* "+Os_version.getTitle()+"\n");

} return null;

} @Override

protected void onPostExecute(Void aVoid) {



txtSubject.setText("Subject: "+beanSubject.getSubject());

txtPrice.setText("price: "+beanSubject.getPrice());

txtAuthor.setText("Author: "+beanSubject.getAuthor());

txtOs_versionList.setText("Os_version: "+"\n"+Os_versionList);



Step 8:

Run the application to see the result.

Thats it .We are able to fetch the data from the URL using HTTP request through GSON in android app

Happy Coding!Happy Development.

Alll the world is a laboratory to the inquiring mind! Join Us


Nilesh said…
Awesome!!! Awesome !! thank you for posting this.. Very helpful!!
Zen said…
Lovely tutorial!! I signed up for your blog newsletter please continue posting like this!
Manco said…
Really nice ! The series is going really well keep up the good work buddy

Popular posts from this blog

Android Navigation Drawer sliding menu with fragments

8 Effective plots with Matplotlib and Pandas Dataframe

Step by Step Guide to Setup Android 5.0 Lollipop Sdk in Eclipse