FooService.java
public interface FooService {
...
@GET("allVideoInfo/id/{videoid}")
Observable<VideoModel> getAllVideoInfo(@Path("videoid") String videoId);
...
}
FooApiManager.java
public class FooApiManager {
private final FooService fooService;
private static FooApiManager instance;
private Context context;
private Gson gson;
public static OkHttpClient client;
...
public static FooApiManager with(Context context) {
if (instance == null) {
synchronized (FooApiManager.class) {
new FooApiManager(context);
}
}
return instance;
}
private FooApiManager(Context context) {
this.context = context;
gson = new GsonBuilder().setLenient().create();
if (client == null) {
client = new OkHttpClient.Builder()
.build();
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://example.com/api/android/")
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
fooService = retrofit.create(FooService.class);
instance = this;
}
...
public Observable<VideoModel> getAllVideoInfo(String videoId) {
return fooService
.getAllVideoInfo(videoId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
...
}
FooActivity.java
...
FooApiManager.with(getActivity())
.getAllVideoInfo(video.getId())
.subscribe(fullVideoModel -> {
...
}, error -> { // MUST be handled when request fails or invalid response
// error.getMessage();
// error.printStackTrace();
...
});
...