Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jaimetoca/retrofitottoexample
This is just an example of how you can use Retrofit and Otto libraries in your android projects.
https://github.com/jaimetoca/retrofitottoexample
Last synced: 7 days ago
JSON representation
This is just an example of how you can use Retrofit and Otto libraries in your android projects.
- Host: GitHub
- URL: https://github.com/jaimetoca/retrofitottoexample
- Owner: JaimeToca
- License: apache-2.0
- Created: 2015-06-19T16:52:40.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-03-23T08:24:52.000Z (almost 9 years ago)
- Last Synced: 2024-10-29T05:22:35.517Z (about 2 months ago)
- Language: Java
- Homepage:
- Size: 48.8 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Retrofit-Otto-Example
(Note: Otto is deprecated due to RxJava)
This is just a basic example of how you can use Retrofit and Otto libraries in your android projects. I decided to work with [themovieDB](https://www.themoviedb.org/), an opensource database for TV Shows, Movies and Actors. The purpose of this project is to show the last 20 most famous actors using retrofit wich works with Rest-Json among others, also Otto is used to notify the main activity that the information is ready and send the actors aswell.**MovieDB API**
This is how the request looks like for getting the 20 most popular actors :http://api.themoviedb.org/3/person/popular?api_key=XXXXX
check out the JSON response [here](http://docs.themoviedb.apiary.io/#reference/people/personpopular/get)
**When to use Otto?**
Otto is a great way to communicate between different components in your project. Events are sent through a bus and the classes interested to some specific events have to suscribe to them. I recommend you play with Otto, but keep in mind that sometimes is not the best option, specially when it comes to nesting events, you can really get in trouble and become mad with the debugging. In my opinion, Otto is good, but in the end I would recommend to use simple listeners / Threads or even better RxJava (this repository was created before I learnt RxJava) , although some of them might require more code, will make things eassier to understand .In this project, there are 2 modules and once the information is downloaded the rest module needs to comunicate with the main activity (the one that will show the information ).
First of all, a singleton instance of the Bus class will be created in order to provide access to it for the android components :MainActivity.class
private Bus bus;
@Override
protected void onCreate(Bundle savedInstanceState) {
bus = new Bus (ThreadEnforcer.ANY);
bus.register(this);
.....
}MainActivity Class will wait for events, the registration is done via register and @Subcribe annotation on a public single parameter method.
MainActivity.Class
@Subscribe
public void ActorsReceived(ActorsWrapper actorsWrapper) {
downloadButton.setText(R.string.done);
notificationText.setText(R.string.downloaded);
showActors(actorsWrapper);
}Then, if the information is ready the main activity is notified through the bus using post
RestActorSource.class
@Override
public void sendActorsToActivity (ActorsWrapper actorsResponse) {
bus.post(actorsResponse);
}//RetrofitCallback
public Callback retrofitCallback = new Callback() {
@Override
public void success(Object o, Response response) {if (o instanceof ActorsWrapper){
sendActorsToActivity((ActorsWrapper) o);
}
}@Override
public void failure(RetrofitError error) {
Log.e("Callback failure", ""+ error.getMessage());
}
};**Project structure**
*APP*
*MainActivity*
|-> Entities (*Actor, ActorsWrapper, Known_For*)
|-> Rest (*ActorDatabaseApi, MediaSource, RestActorSource*)
**References:**
http://square.github.io/otto/
http://square.github.io/retrofit/
http://docs.themoviedb.apiary.io/
https://www.themoviedb.org/documentation/api
https://github.com/saulmm/Material-Movies
http://www.vogella.com/tutorials/JavaLibrary-EventBusOtto/article.html# Licence
Copyright 2015 Jaime Toca
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.