Created
January 23, 2016 19:11
-
-
Save kokeroulis/af2355f3552d2b9bee5b to your computer and use it in GitHub Desktop.
OkHttp nested requests
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
// this is a singleton and it takes instance from dagger! | |
@Inject OkHttpClient client; | |
Subscription sub; | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
// Stop the request if our activity/fragment has been destroyed. | |
if (sub != null && !sub.isUnsubscribed()) { | |
sub.unsubscribe(); | |
} | |
} | |
public void insideSomeFunction() { | |
sub = chainableRequests() | |
.subscribe(new Action1<String>() { | |
@Override | |
public void call(String resultFromMultipleRequests) { | |
myTextView.setText(resultFromMultipleRequests); | |
} | |
}, new Action1<Throwable>() { | |
@Override | |
public void call(Throwable throwable) { | |
Toast.makeText(getBaseContext(), "error during our request:" + | |
throwable.getMessage(), Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
public Observable<String> chainableRequests() { | |
return getHello() // execute the first request | |
.flatMap(new Func1<String, Observable<String>>() { | |
@Override | |
public Observable<String> call(String helloString) { | |
// get the result from the first request and pass it to the second | |
return doSomethingWithTheHello(helloString); | |
} | |
}) | |
// Go outside of the main thread in order to prevent ANR | |
.subscribeOn(Schedulers.io()) | |
// Once everything is finished go back to main thread. | |
.observeOn(AndroidSchedulers.mainThread()); | |
} | |
public Observable<String> getHello() { | |
return Observable.create(new Observable.OnSubscribe<String>() { | |
@Override | |
public void call(Subscriber<? super String> subscriber) { | |
if (subscriber.isUnsubscribed()) { | |
return; | |
} | |
try { | |
Request request = new Request.Builder() | |
.url("http://publicobject.com/helloworld.txt") | |
.build(); | |
Response response = client.newCall(request).execute(); | |
if (!response.isSuccessful()) { | |
subscriber.onError(new Throwable("Unexpected code " + response)); | |
} else { | |
subscriber.onNext(response.body().string()); | |
subscriber.onCompleted(); | |
} | |
} catch (IOException error) { | |
subscriber.onError(error); | |
} | |
} | |
}); | |
} | |
public Observable<String> doSomethingWithTheHello(String hello) { | |
return Observable.create(new Observable.OnSubscribe<String>() { | |
@Override | |
public void call(Subscriber<? super String> subscriber) { | |
if (subscriber.isUnsubscribed()) { | |
return; | |
} | |
try { | |
MediaType textType = MediaType.parse("text/html; charset=utf-8"); | |
Request request = new Request.Builder() | |
.url("https://api.github.com/markdown/raw") | |
.post(RequestBody.create(textType, hello)) | |
.build(); | |
Response response = client.newCall(request).execute(); | |
if (!response.isSuccessful()) { | |
subscriber.onError(new Throwable("Unexpected code " + response)); | |
} else { | |
subscriber.onNext(response.body().string()); | |
subscriber.onCompleted(); | |
} | |
} catch (IOException error) { | |
subscriber.onError(error); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment