This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.annotations.SerializedName; | |
public class BoredActivities { | |
@SerializedName("accessibility") | |
private Double mAccessibility; | |
@SerializedName("activity") | |
private String mActivity; | |
@SerializedName("key") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.reactivex.Flowable; | |
import io.reactivex.Observable; | |
import io.reactivex.Single; | |
import retrofit2.http.GET; | |
public interface BoredActivitiesAPI { | |
@GET("activity") | |
Observable<BoredActivities> getObservableResponse(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class APIExample { | |
BoredActivitiesAPI sampleAPI; | |
public APIExample(BoredActivitiesAPI boredActivitiesAPI) { | |
this.sampleAPI = boredActivitiesAPI; | |
} | |
public Flowable<String> makeAPICall() { | |
return sampleAPI.getFlowableResponse() | |
.onErrorReturn(new Function<Throwable, BoredActivities>() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class APIExample { | |
BoredActivitiesAPI sampleAPI; | |
LifecycleOwner lifecycleOwner; | |
public APIExample(BoredActivitiesAPI boredActivitiesAPI, LifecycleOwner lifecycleOwner) { | |
this.sampleAPI = boredActivitiesAPI; | |
this.lifecycleOwner = lifecycleOwner; | |
} | |
public LiveData<String> makeAPICall() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |