Skip to content

Instantly share code, notes, and snippets.

View BalakrishnanPT's full-sized avatar
🎯
Focusing

Balakrishnan BalakrishnanPT

🎯
Focusing
  • Edison Interactive
  • Remote
View GitHub Profile
// 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'
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();
import com.google.gson.annotations.SerializedName;
public class BoredActivities {
@SerializedName("accessibility")
private Double mAccessibility;
@SerializedName("activity")
private String mActivity;
@SerializedName("key")
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.Single;
import retrofit2.http.GET;
public interface BoredActivitiesAPI {
@GET("activity")
Observable<BoredActivities> getObservableResponse();
public class APIExample {
BoredActivitiesAPI sampleAPI;
public APIExample(BoredActivitiesAPI boredActivitiesAPI) {
this.sampleAPI = boredActivitiesAPI;
}
public Flowable<String> makeAPICall() {
return sampleAPI.getFlowableResponse()
.onErrorReturn(new Function<Throwable, BoredActivities>() {
public class APIExample {
BoredActivitiesAPI sampleAPI;
LifecycleOwner lifecycleOwner;
public APIExample(BoredActivitiesAPI boredActivitiesAPI, LifecycleOwner lifecycleOwner) {
this.sampleAPI = boredActivitiesAPI;
this.lifecycleOwner = lifecycleOwner;
}
public LiveData<String> makeAPICall() {
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;
// 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 {
// 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 {