As a follow up to my earlier post, I would like to make a jar library available that should allow you to access any RESTFull service that uses JSON. Simply import this jar file into your project and you should be good to go.

Please note that the attached jar file contains a compiled copy of the GSON library available at

Click here to download the jar file.

Once you have downloaded the above file and added it to your project you may use the following code to access a JSON web service.

The example shown here returns a collection of JSON objects in the form

{"alertid":"1","alerttext":"This is test","alertdate":"2010-02-11 09:03:40"},
{"alertid":"2","alerttext":"Another alert","alertdate":"2010-02-11 09:11:04"},
{"alertid":"3","alerttext":"This is third test","alertdate":"2010-02-11 11:00:57"},
{"alertid":"4","alerttext":"message from starbucks","alertdate":"2010-02-11 18:00:22"}]

The class I implemented to hold these object is as follows

public class alerts {
	public int alertid;
	public String alerttext;
	public String alertdate;
	public String toString()
		return "Alert ID: "+alertid+ " Alert Text: "+alerttext+ " Alert Date: "+alertdate;

With the above class implemented, we can used the provided library to access the web-service available at and parse its JSON repose back into our above mentioned java class.

//Instantiate your web service call
WebService webService = new WebService("");

//Pass the parameters needed to the service
Map params = new HashMap();
//Please note that this service doesn't have any required parameters and I am only passing these as an example
params.put("firstName", "yName");
params.put("isActive", false);
//If you need to pass a JSON object as a parameter you may do so as follows
params.put("requiredObject", WebService.Object(myObject));

//Using GET
String response = webService.webGet("", params);
//You may pass it a method name if there is one as such
//String response = webService.webGet("METHODNAME", params);

//To retrieve an collection of objects
Type collectionType = new TypeToken>(){}.getType();
List lst= new Gson().fromJson(response, collectionType);

//Note that the above object alerts has to match the structure of our JSON object, please see the beginning of the post for an example. 

//To retrieve a single item instead of a collection
alerts alert = new Gson().fromJson(response, alerts.class);

//To use POST instead of get 
webService.webInvoke("", params);

I hope this helps, feel free to use this library anywhere you want, the source code for it is available in an earlier post so if you’d like to modify it feel free to do so.

Be Sociable, Share!

Tags: , , ,

15 Comments on Android accessing RESTFull JSON Services Library

  1. alin says:

    This is the greatest post I have found regarding android and returning json values in a POJO. Thank you very much for sharing your knowledge.

    I need to send headers to the request. What is the best way to do this ?

  2. CasaDelGato says:

    Is a .jar of the source code available?

  3. aziz says:

    it seems simple. but how can i bring these data to my android application. how can i but at least one of these data in to a text field.


  4. zenzo says:

    hi, goog work
    i have this error :

    Description Resource Path Location Type
    Conversion to Dalvik format failed with error 1 Items Unknown Android Packaging Problem


  5. Atii Herit says:

    I am using mysql database for data.can you tell me how ? i can found the name of the service ?

  6. Sougata Chandra says:

    Hi ,
    I have a question.
    the JSON webservice is working fine for me.
    Now what I need to do is that from the result, instead of displaying it in the list, i need to insert each of the entry to a sqllite data table.
    Any idea how i should do that ?



  7. Chris Keele says:

    This code has worked like a charm for me in the past, which is great because I’m very new to the droid scene. However, the JSON I’m dealing with in my latest project is structured slightly differently than your example:
    “excerpt”:”The University issued an emergency alert text message reporting a hazardous chemical spill at Cho…”,
    “title”:”Hazardous chemical spill reported at Choppin Hall”,


    As opposed to your alerts:
    “alerttext”:”This is test”,
    “alertdate”:”2010-02-11 09:03:40″
    “alerttext”:”This is test”,
    “alertdate”:”2010-02-11 09:03:40″


    I’ve poked around the code a little but I’m having a hard time figuring out how to parse around the extra layer. Suggestions?

  8. John says:


    Do you know how we would set the connection timeout using your library? … Trying to see if it’s possible.

    If you have any good resources for best practices on managing network connections I’d appreciate that too … Just now getting into retrieving data and having the typical network timeout issues etc.


  9. Goutam Khandelwal says:

    Hi Jose,

    A really good post.
    Can we use this for the OData Service as well?

    Any recommendation.


  10. Lars says:

    Can I use this for a google cloud storage bucket object and if so how? I want to create global highscores. Since it´s all new to me, I´m not getting any step further.
    Can anyone help me out with an example?

  11. Lars says:

    For android..

  12. Ashish says:

    Very helpful explanation for invoking RESTful webservice and retrieving data in json format. Thanks a lot