Reactive http is lightweight REST http library with Observable(RxJava) interface.
ReactiveHttpClient client = new ReactiveHttpClient(new OkHttpClient(), new Gson(), Schedulers.executor(Executors.newFixedThreadPool(3), null, false);
client.create()
.post("https://api.bar.com/do/%s/%s", "abc", "cba")
.query("foo", "bar")
.set("Authorization", "foo:bar")
.data(new MyData(1, "2"))
.observe(MyResponse.class)
.subscribe(new Action1<MyResponse>() {
@Override
public void call(MyResponse response) {
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
if (throwable instanceof HttpResponseException) {
HttpResponseException hre = (HttpResponseException) throwable;
GitHubError error = hre.getError(GithubApiError.class).message);
}
}
}
);
OkHttp has a problem that causes this crash on Android. The official workaround from square is to do the following just after OkHttpClient instance is created:
OkHttpClient okHttpClient = new OkHttpClient();
URL.setURLStreamHandlerFactory(okHttpClient);
private HttpRequest createRequest() {
return client.create()
.set("Authorization", "foo:bar")
.set("Accept-Language", "en-US")
}
private Observable<Foo> requestFoo() {
return createRequest()
.get("https://api.bar.com/foo")
.observe(Foo.class);
}
client.create()
.get("https://api.bar.com/do/")
.observe()
.subscribe(new Action1<HttpResponse>() {
@Override
public void call(HttpResponse response) {
}
});
client.create()
.get("https://api.bar.com/do/")
.observeAsString()
.subscribe(new Action1<String>() {
@Override
public void call(String response) {
}
});
client.create()
.post("https://api.imgur.com/3/image")
.file("image/jpeg", file)
.set("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
.observe(ImgurResponse.class)
.subscribe(new Action1<ImgurResponse>() {
@Override
public void call(ImgurResponse response) {
...
}
}
);
public class ConsoleLog implements HttpLog {
@Override
public void log(String message) {
for (int i = 0, len = message.length(); i < len; i += LOG_CHUNK_SIZE) {
int end = Math.min(len, i + LOG_CHUNK_SIZE);
System.out.println(message.substring(i, end));
}
}
}
// supply log class and set logging enabled param to true
ReactiveHttpClient client = new ReactiveHttpClient(new OkHttpClient(), new Gson(), Schedulers.currentThread(), new ConsoleLog(), true);
<dependency>
<groupId>com.lyft</groupId>
<artifactId>reactivehttp</artifactId>
<version>0.0.1</version>
</dependency>
compile 'com.lyft:reactivehttp:0.0.1'
For bugs, questions and discussions please use the Github Issues.
- Http Request by Kevin Sawicki
- Retrofit by Square
- Super Agent by TJ Holowaychuk