Some trivial helpers for Android.
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
compile 'io.reactivex.rxjava2:rxjava:2.x.x' // for using RxHttpClient
// or compile 'com.parse.bolts:bolts-tasks:1.4.0' if you use BoltsHttpClient
compile 'com.github.yusukeiwaki.android-helper:okhttp3_helper:0.0.0'
}
Just wrapping OkHttp asynchronous callback with rx.Single
.
HTTP error statuses (401, 403, 404, 500, ...) are notified with onSuccess
callback.
Request req = new Request.Builder()
.url("http://www.yahoo.co.jp/")
.build();
RxHttpClient client = new RxHttpClient(new OkHttpClient());
client.execute(req)
.filter(response -> response.isSuccessful())
.map(response -> response.body().string())
.subscribe(body -> {
TextView textView = (TextView) findViewById(R.id.text);
textView.setText(body);
});
Wrapping OkHttp asynchronous callback with bolts.Task
.
Unlike RxHttpClient, HTTP error statuses are notified as HttpError
callback.