Skip to content

Instantly share code, notes, and snippets.

@amitshekhariitbhu
Last active January 26, 2017 09:45
Show Gist options
  • Save amitshekhariitbhu/8ad392042807b5656a01ead8a39a5908 to your computer and use it in GitHub Desktop.
Save amitshekhariitbhu/8ad392042807b5656a01ead8a39a5908 to your computer and use it in GitHub Desktop.
/*-------------- Getting the userList ----------------*/
RxAndroidNetworking.get("http://api.localhost.com/getAllUsers/{pageNumber}")
.addPathParameter("pageNumber", "0")
.addQueryParameter("limit", "3")
.build()
.getParseObservable(new TypeToken<List<User>>() {}) // This returns you Observable
.subscribeOn(Schedulers.io()) // do the network call on another thread
.observeOn(AndroidSchedulers.mainThread()) // return the result in mainThread
.subscribe(new Observer<List<User>>() {
@Override
public void onCompleted() {
// do anything onComplete
}
@Override
public void onError(Throwable e) {
// handle error
}
@Override
public void onNext(List<User> users) {
// do anything with response
Log.d(TAG, "userList size : " + users.size());
for (User user : users) {
Log.d(TAG, "id : " + user.id);
Log.d(TAG, "firstname : " + user.firstname);
Log.d(TAG, "lastname : " + user.lastname);
}
}
});
/*-------------- Getting an user ----------------*/
RxAndroidNetworking.get("http://api.localhost.com/getAnUser/{userId}")
.addPathParameter("userId", "1")
.build()
.getParseObservable(new TypeToken<User>() {}) // This returns you Observable
.subscribeOn(Schedulers.io()) // do the network call on another thread
.observeOn(AndroidSchedulers.mainThread()) // return the result in mainThread
.subscribe(new Observer<User>() {
@Override
public void onCompleted() {
// do anything onComplete
}
@Override
public void onError(Throwable e) {
// handle error
}
@Override
public void onNext(User user) {
// do anything with response
Log.d(TAG, "id : " + user.id);
Log.d(TAG, "firstname : " + user.firstname);
Log.d(TAG, "lastname : " + user.lastname);
}
});
/*-- Note : TypeToken and getParseObservable is important here --*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment