-
Notifications
You must be signed in to change notification settings - Fork 0
Home
In this example, we will look at Retrofit, an HTTP client open sourced by Square which has built-in bindings with RxJava to interact with GitHub’s API. Specifically, we’ll create a simple app that presents all the starred repositories for a user given a GitHub username.
As you might imagine, there are many use cases for RxJava but, in this example, let’s take a look at one specific case: using Observable objects as part of the network stack.
- Start by creating a new Android project and naming it anything like RxJavaRetrofit Demo
- In the Target Android Devices screen, keep Phone and Tablet selected and set the minimum SDK level of 21or 17. Feel free to set it to a lower/higher API level but, for this example, API level 21 will suffice.
- Select Empty Activity in the next prompt.
In the last step, keep the Activity Name as MainActivity and generate a layout file activity_main.
Include RxJava, RxAndroid, and the Retrofit library in app/build.gradle. Note that including RxAndroid implicitly also includes RxJava. It is best practice, however, to always include those two libraries explicitly since RxAndroid does not always contain the most up-to-date version of RxJava. Explicitly including the latest version of RxJava guarantees use of the most up-to-date version.
dependencies { compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'io.reactivex:rxandroid:1.2.0' compile 'io.reactivex:rxjava:1.1.8' // ...other dependencies }
Create a GitHubRepo data object class. This class encapsulates a repository in GitHub (the network response contains more data but we’re only interested in a subset of that). public class GitHubRepo {
public final int id;
public final String name;
public final String htmlUrl;
public final String description;
public final String language;
public final int stargazersCount;
public GitHubRepo(int id, String name, String htmlUrl, String description, String language, int stargazersCount) {
this.id = id;
this.name = name;
this.htmlUrl = htmlUrl;
this.description = description;
this.language = language;
this.stargazersCount = stargazersCount;
}
}
Create the **GitHubService **interface. We will pass this interface into Retrofit and Retrofit will create an implementation of GitHubService. public interface GitHubService { @GET("users/{user}/starred") Observable<List> getStarredRepositories(@Path("user") String userName); }
Create the **GitHubClient **class. This will be the object we will interact with to make network calls from the UI level.
**Note: When constructing an implementation of GitHubService through Retrofit, we need to pass in an **RxJavaCallAdapterFactory **as the call adapter so that network calls can return Observable objects (passing a call adapter is needed for any network call that returns a result other than a Call).
public class GitHubClient {
private static final String GITHUB_BASE_URL = "https://api.github.com/";
private static GitHubClient instance;
private GitHubService gitHubService;
private GitHubClient() {
final Gson gson =
new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
final Retrofit retrofit = new Retrofit.Builder().baseUrl(GITHUB_BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
gitHubService = retrofit.create(GitHubService.class);
}
public static GitHubClient getInstance() {
if (instance == null) {
instance = new GitHubClient();
}
return instance;
}
public Observable<List<GitHubRepo>> getStarredRepos(@NonNull String userName) {
return gitHubService.getStarredRepositories(userName);
}
}
Glue Everything Together Create a ListAdapter that is in charge of binding GitHubRepo objects into ListView items
Glue everything together in MainActivity. This is essentially the Activity that gets displayed when we first launch the app. In here, we ask the user to enter their GitHub username, and finally, display all the starred repositories by that username.
private void getStarredRepos(String username) { subscription = GitHubClient.getInstance() .getStarredRepos(username) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<List>() { @Override public void onCompleted() { Log.d(TAG, "In onCompleted()"); }
@Override public void onError(Throwable e) {
e.printStackTrace();
Log.d(TAG, "In onError()");
}
@Override public void onNext(List<GitHubRepo> gitHubRepos) {
Log.d(TAG, "In onNext()");
adapter.setGitHubRepos(gitHubRepos);
}
});
}
}
Running the app should present a screen with an input box to enter a GitHub username. Searching should then present the list of all starred repos.
For Tutorial on RxJava-Android Click here.