Skip to content

Instantly share code, notes, and snippets.

@DavidMihola
Last active December 14, 2020 17:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidMihola/17a6ea373b9312fb723b to your computer and use it in GitHub Desktop.
Save DavidMihola/17a6ea373b9312fb723b to your computer and use it in GitHub Desktop.
A simple test for Retrofit2, RxJava and HTTP error codes
package com.example;
import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.RxJavaCallAdapterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Url;
import rx.Observable;
import rx.Subscriber;
public class RetrofitErrorCodeTest {
private interface RetrofitInterface {
@GET("/{code}")
Observable<Response<ResponseBody>> get(@Path("code") int code);
}
public static void main(String[] args) {
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://httpstat.us")
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
final RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);
retrofitInterface.get(200)
.subscribe(new Subscriber<Response<ResponseBody>>() {
@Override
public void onCompleted() {
System.out.println("onCompleted");
}
@Override
public void onError(Throwable e) {
System.out.println("onError");
e.printStackTrace();
}
@Override
public void onNext(Response<ResponseBody> stringResponse) {
System.out.println(String.format("onNext: %s", String.valueOf(stringResponse.code())));
}
});
}
}
@wertalp
Copy link

wertalp commented Dec 14, 2020

Merci for simple and straight code sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment