A very simple ptoject/tutorial to show how to use Restlet and Gson libs under Android
It's a project tutorial that will help you to understand how to use Restlet framework (http:https://restlet.com/)
and how to implement a JSON deserialization by using Gson Google framework (https://code.google.com/p/google-gson/).
You can use this project as template to develop your custom resource consumer.
With this class set you can consume at the same time resources from different uri service.
In this example we will consume in the same project resources from RestLet Demo service and from GitHub API service.
I developed this project wih Android Studio, however you can setup this code on your favourite IDE.
——>restlet.sdk
———> entities
———> http
—————> clients
—————> resources
———> interfaces
———> events
———> generics
- restlet.sdk.entities
contains all classes to map JSON items of REST resources
- restlet.sdk.http.clients
contains Restlet ClientResource singleton classes of REST resources
- restlet.sdk.http.resources
contains helper classes that allow to consume REST resources with one or more operations for different REST path
- restlet.sdk.interfaces
IClientResourceCallback: defines event method signatures for Restlet http callbacks
- restlet.sdk.events
ClientResourceCallbackEventsManager: implements and dispatches Restlet http callbacks
- restlet.sdk.generics
JsonToObject: is a generic T class tha allow to deserialize a JSON string into a custom class
To test this class set, I developed two REST service
- Restlet Contacts API demo service: http:https://restlet-example-serialization.appspot.com/contacts/123
- GitHub User repositories API:https://api.github.com/users/GIT_HUB_USER_HERE/repos
Inside ContactResource class you will find a static method (getContact) that allow to consume the Contact.
See loadContact nethod inside MainActivity class.
Instead to consume GitRepos resource you have to refer to GitHubResource class and call ths static method getUserRepos.
See loadGitHubRepos nethod inside MainActivity class.
- add your entities classes and map these classes with the corresponding JSON structure.
If the JSON response is an array you have to develop a class with a List attribute (see Contacts)
- add your EntityClientResource.
For further details see ContactClientResource and GitHubClientResource classes.
However you create these classe by copying from ContactClientResource ord GitHubClientResource and by changing: baseUri value, _instance type and if you need to add custom settings you have to add them to getResource method.
- add your EntityResource classes to define methods for REST operations
For further details see ContactResource and GitHubResource classes.