Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

BalaKrishnan BalakrishnanPT

🎯
Focusing
View GitHub Profile
View BlockingGetExample.java
// 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 {
View BlockingGetExample.java
// 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 {
View APIExample.java
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.LiveDataReactiveStreams;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;
import com.toyaround.android.communities.questionsActivity.network.BoredActivities;
import com.toyaround.android.communities.questionsActivity.network.BoredActivitiesAPI;
import io.reactivex.functions.Function;
View APIExample.java
public class APIExample {
BoredActivitiesAPI sampleAPI;
LifecycleOwner lifecycleOwner;
public APIExample(BoredActivitiesAPI boredActivitiesAPI, LifecycleOwner lifecycleOwner) {
this.sampleAPI = boredActivitiesAPI;
this.lifecycleOwner = lifecycleOwner;
}
public LiveData<String> makeAPICall() {
View APIExample.java
public class APIExample {
BoredActivitiesAPI sampleAPI;
public APIExample(BoredActivitiesAPI boredActivitiesAPI) {
this.sampleAPI = boredActivitiesAPI;
}
public Flowable<String> makeAPICall() {
return sampleAPI.getFlowableResponse()
.onErrorReturn(new Function<Throwable, BoredActivities>() {
View BoredActivitiesAPI.java
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.Single;
import retrofit2.http.GET;
public interface BoredActivitiesAPI {
@GET("activity")
Observable<BoredActivities> getObservableResponse();
View BoredActivities.java
import com.google.gson.annotations.SerializedName;
public class BoredActivities {
@SerializedName("accessibility")
private Double mAccessibility;
@SerializedName("activity")
private String mActivity;
@SerializedName("key")
View RetrofitSetup.java
private Retrofit getRetrofit() {
if(retrofitInstance != null) return retrofitInstance;
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.level(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(logging)
.build();
View build.gradle
// To make API calls
def retrofitVersion = '2.6.0'
// To logs API calls & responses
def logging_version = '4.0.0'
// To use Rx to handle the response and observe the changes in realtime
def rxandroid_version = '2.1.1'
def rxjava_version = '2.1.1'
def rxcalladapter_version = '2.6.0'
You can’t perform that action at this time.