Last active
December 14, 2020 17:59
-
-
Save DavidMihola/17a6ea373b9312fb723b to your computer and use it in GitHub Desktop.
A simple test for Retrofit2, RxJava and HTTP error codes
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
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()))); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Merci for simple and straight code sharing