Skip to content

Instantly share code, notes, and snippets.

@ar-android
Created November 10, 2016 16:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ar-android/0ef66ba37ee767dbb44fe3e45fe6052f to your computer and use it in GitHub Desktop.
Save ar-android/0ef66ba37ee767dbb44fe3e45fe6052f to your computer and use it in GitHub Desktop.
OkHttp With RxAndroid and RxJava
public static Observable<Response> getData() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
final OkHttpClient client = new OkHttpClient();
final Request request = new Request.Builder()
.url("https://github.com/ar-android/panfic/raw/master/Panfic/gen/com/ocit/data.json")
.get()
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "ac8311d5-3876-ea1e-53d3-85f9e397ea21")
.build();
return Observable.create(new Observable.OnSubscribe<Response>() {
@Override public void call(Subscriber<? super Response> subscriber) {
try {
Response response = client.newCall(request).execute();
subscriber.onNext(response);
subscriber.onCompleted();
} catch (IOException e) {
e.printStackTrace();
subscriber.onError(e);
}
}
});
}
@SEOJAEHWA
Copy link

It is helpful for me. thanks a lot.

@ali-moghadam
Copy link

You should call OnComplete() on finally block

try {
// Do stuff
} catch (IOException e){
// Do stuff
} finally {
subscriber.onCompleted();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment