Skip to content

Instantly share code, notes, and snippets.

@BalakrishnanPT
Created October 18, 2019 12:16
Show Gist options
  • Save BalakrishnanPT/ea6281f15f5dc7882c2e74b3ec46b848 to your computer and use it in GitHub Desktop.
Save BalakrishnanPT/ea6281f15f5dc7882c2e74b3ec46b848 to your computer and use it in GitHub Desktop.
// Calling function, This is in Activity or Viewmodel
public void blockingGetExample() {
BoredActivitiesAPI sampleAPI = getRetrofitInstance.create(BoredActivitiesAPI.class);
// Eventhough RxJava call is happening in diffrent thread, Result of the RxJava will be strored in
// `s` and nexr line is executed.
String s = sampleAPI.getSingleResponse()
.onErrorReturn(new Function<Throwable, BoredActivities>() {
@Override
public BoredActivities apply(Throwable throwable) throws Exception {
BoredActivities boredActivities = new BoredActivities();
boredActivities.setLink("");
return boredActivities;
}
})
.map(new Function<BoredActivities, String>() {
@Override
public String apply(BoredActivities boredActivities) throws Exception {
return boredActivities.getLink();
}
})
.subscribeOn(Schedulers.io()).blockingGet();
Log.d("Sample", "blockingGetExample: "+s);
}
// Retrofit Method, interface, name - BoredActivitiesAPI.java
@GET("activity")
Single<BoredActivities> getSingleResponse();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment