Add this in yoursettings.gradle:
maven{url'https://jitpack.io'}If you are usingsettings.gradle.kts, add the following:
maven{setUrl("https://jitpack.io")}Add this in yourbuild.gradle
implementation'com.github.amitshekhariitbhu.Fast-Android-Networking:rx2-android-networking:1.0.4'If you are usingbuild.gradle.kts, add the following:
implementation("com.github.amitshekhariitbhu.Fast-Android-Networking:rx2-android-networking:1.0.4")Do not forget to add internet permission in manifest if already not present
<uses-permissionandroid:name="android.permission.INTERNET"/>Then initialize it in onCreate() Method of application class :
AndroidNetworking.initialize(getApplicationContext());/* * Here we are getting ApiUser Object from api server* then we are converting it into User Object because * may be our database support User Not ApiUser Object* Here we are using Map Operator to do that*/Rx2AndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAnUser/{userId}").addPathParameter("userId","1").build().getObjectObservable(ApiUser.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).map(newFunction<ApiUser,User>(){@OverridepublicUserapply(ApiUserapiUser)throwsException{// here we get ApiUser from serverUseruser=newUser(apiUser);// then by converting, we are returning userreturnuser;}}).subscribe(newObserver<User>(){@OverridepublicvoidonSubscribe(Disposabled){}@OverridepublicvoidonNext(Useruser){}@OverridepublicvoidonError(Throwablee){}@OverridepublicvoidonComplete(){}});/* * Here we are making two network calls * One returns the list of cricket fans* Another one returns the list of football fans* Then we are finding the list of users who loves both*//** This observable return the list of User who loves cricket*/privateObservable<List<User>>getCricketFansObservable(){returnRx2AndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAllCricketFans").build().getObjectListObservable(User.class);}/** This observable return the list of User who loves Football*/privateObservable<List<User>>getFootballFansObservable(){returnRx2AndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAllFootballFans").build().getObjectListObservable(User.class);}/** This do the complete magic, make both network call* and then returns the list of user who loves both* Using zip operator to get both response at a time*/privatevoidfindUsersWhoLovesBoth(){// here we are using zip operator to combine both requestObservable.zip(getCricketFansObservable(),getFootballFansObservable(),newBiFunction<List<User>,List<User>,List<User>>(){@OverridepublicList<User>apply(List<User>cricketFans,List<User>footballFans)throwsException{List<User>userWhoLovesBoth=filterUserWhoLovesBoth(cricketFans,footballFans);returnuserWhoLovesBoth;}}).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(newObserver<List<User>>(){@OverridepublicvoidonSubscribe(Disposabled){}@OverridepublicvoidonNext(List<User>users){// do anything with user who loves both}@OverridepublicvoidonError(Throwablee){}@OverridepublicvoidonComplete(){}});}privateList<User>filterUserWhoLovesBoth(List<User>cricketFans,List<User>footballFans){List<User>userWhoLovesBoth=newArrayList<>();// your logic to filter who loves bothreturnuserWhoLovesBoth;}