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 http://code.google.com/p/google-gson/.
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; @Override 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 http://www.sumasoftware.com/alerts/GetAlerts.php and parse its JSON repose back into our above mentioned java class.
import josecgomez.com.android.webservices.json.WebService; //Instantiate your web service call WebService webService = new WebService("http://www.sumasoftware.com/alerts/GetAlerts.php"); //Pass the parameters needed to the service Mapparams = new HashMap (); params.put("var",""); //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.
Tags: Android, JSON, RESTFull, Web Services
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 ?
Is a .jar of the source code available?
There is Jar you can download at this post http://www.josecgomez.com/2010.....s-library/ is that what you are looking for?
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.
thanks
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
thank’s
I am using mysql database for data.can you tell me how ? i can found the name of the service ?
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 ?
Thanks,
Sougata
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:
[
{“article”:
{“author_id”:49,
“category”:”news”,
“created_at”:”2011-05-18T22:00:07Z”,
“excerpt”:”The University issued an emergency alert text message reporting a hazardous chemical spill at Cho…”,
“id”:1942,
“published”:”2011-05-18T21:50:52Z”,
“title”:”Hazardous chemical spill reported at Choppin Hall”,
“updated_at”:”2011-05-18T22:00:07Z”,
},
]
As opposed to your alerts:
[
{“alertid”:”1″,
“alerttext”:”This is test”,
“alertdate”:”2010-02-11 09:03:40″
},
{“alertid”:”2″,
“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?
Resolved.
Glad you figured it out. Can you post your solution for other users? Thanks!
Jose,
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.
Thanks!
Hi Jose,
A really good post.
Can we use this for the OData Service as well?
Any recommendation.
Regards,
Goutam
Hi,
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?
Thanks
For android..
Very helpful explanation for invoking RESTful webservice and retrieving data in json format. Thanks a lot