How To Make A Http Request To Check A Content Type With Rxjava 2?
I need to get the content type from a specific URL. I know that we can do it by simply coding: URL url = new URL('https://someurl.com'); HttpURLConnection connection = (HttpURLConn
Solution 1:
Use RxJava just operator to leave main thread and continue the process on thread from computation scheduler and then use flatMap to make http call and find content type, network calls should run on threads from IO scheduler and finally observe on main thread and subscribe to result.
Observable.just(1).subscribeOn(Schedulers.computation())
.flatMap(dummyValueOne -> {
return Observable.just(getContentType).subscribeOn(Schedulers.io());
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {
@Override
public void accept(String contentType) throws Exception {
//do nextsteps with contentType, you can even update UI here as it runs on main thread
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e("GetContentType", "exception getting contentType", throwable);
}
}));
Post a Comment for "How To Make A Http Request To Check A Content Type With Rxjava 2?"