Created
April 24, 2016 13:44
-
-
Save KimuraTakaumi/fbc5cc59582be2422e0399ebec6e6ecf to your computer and use it in GitHub Desktop.
RxJavaのmapでのエラー処理(onError)
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
private void checkRxJavaOnError() { | |
String[] urls = {"https://www.google.co.jp/?gws_rd=ssl", | |
"http://techium.hatenablog.com/", | |
"http://localhost/"}; | |
Observable | |
.from(urls) | |
.subscribeOn(Schedulers.newThread()) | |
.map(new Func1<String, Boolean>() { | |
@Override | |
public Boolean call(String s) { | |
Log.d("techium", s); | |
String result = null; | |
Request request = new Request.Builder() | |
.url(s) | |
.get() | |
.build(); | |
OkHttpClient client = new OkHttpClient.Builder() | |
.connectTimeout(10, TimeUnit.SECONDS) | |
.writeTimeout(10, TimeUnit.SECONDS) | |
.readTimeout(10, TimeUnit.SECONDS) | |
.build(); | |
try { | |
Response response = client.newCall(request).execute(); | |
result = response.body().string(); | |
} catch (IOException e) { | |
throw Exceptions.propagate(e); | |
} | |
return true; | |
} | |
}).subscribe(new Observer<Boolean>() { | |
@Override | |
public void onCompleted() { | |
Log.d("techium", "onCompleted"); | |
} | |
@Override | |
public void onError(Throwable e) { | |
Log.d("techium", "onError"); | |
e.printStackTrace(); | |
} | |
@Override | |
public void onNext(Boolean b) { | |
Log.d("techium", "onNext " + b); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment