Retrofit is a type-safe REST client library for Android and Java that allows defining REST APIs as Java interfaces. It simplifies HTTP communication by converting remote APIs into declarative interfaces. It supports synchronous, asynchronous, and observable API consumption. The Retrofit library was created by Square.
2. Retrofit
● Retrofit turns your REST API into a Java
interface.
● Simplifies HTTP communication by turning
remote APIs into declarative, type-safe
interfaces
● By Square (Jake Wharton)
3. Declaring API
Synchronous
public interface GitHubService {
@GET("/repos/{owner}/{repo}
/contributors")
List<Contributor> contributors(
@Path("owner") String owner,
@Path("repo") String repo
);
}
13. Consume API (con.)
Observable
Observable<List<Contributor>> ob = restAdapter.
contributors("square", "okhttp");
Subscribtion s = AppObservable.bindActivity(this, ob,
new Action1<List<Contributor>>() {
@Override
public void call(List<Contributor> contributors) {
// TODO
}
);
s.unsubscribe();