Skip to content

Instantly share code, notes, and snippets.

Last active September 11, 2018 10:47
Show Gist options
  • Save NightlyNexus/7ae9402c048aa5c103eee68a69c99d02 to your computer and use it in GitHub Desktop.
Save NightlyNexus/7ae9402c048aa5c103eee68a69c99d02 to your computer and use it in GitHub Desktop.
ConverterCallAdapterFactory uses Retrofit's CallAdapter to pass the okhttp3.Request to a serializer like Jsoup. Idea and corrections from
* Copyright (C) 2017 Eric Cochran
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.Request;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.CallAdapter;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public final class ConverterCallAdapterFactory extends CallAdapter.Factory {
public interface ResponseBodyConverter<T> {
T convert(Request request, ResponseBody body) throws IOException;
interface Factory {
ResponseBodyConverter<?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit);
private final ResponseBodyConverter.Factory converterFactory;
public ConverterCallAdapterFactory(ResponseBodyConverter.Factory converterFactory) {
this.converterFactory = converterFactory;
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
@SuppressWarnings("unchecked") final CallAdapter<Object, ?> delegate =
(CallAdapter<Object, ?>) retrofit.nextCallAdapter(this, returnType, annotations);
@SuppressWarnings("unchecked") final ResponseBodyConverter<Object> converter =
(ResponseBodyConverter<Object>) converterFactory.responseBodyConverter(
delegate.responseType(), annotations, retrofit);
return new CallAdapter<ResponseBody, Object>() {
@Override public Type responseType() {
return ResponseBody.class;
@Override public Object adapt(Call<ResponseBody> call) {
return delegate.adapt(new ConverterCall<>(call, converter));
private static final class ConverterCall<T> implements Call<T> {
private final Call<ResponseBody> delegate;
final ResponseBodyConverter<T> converter;
ConverterCall(Call<ResponseBody> delegate, ResponseBodyConverter<T> converter) {
this.delegate = delegate;
this.converter = converter;
@Override public Response<T> execute() throws IOException {
Response<ResponseBody> response = delegate.execute();
okhttp3.Response raw = response.raw();
if (raw.isSuccessful()) {
return Response.success(converter.convert(delegate.request(), response.body()), raw);
return Response.error(raw.body(), raw);
@Override public void enqueue(final Callback<T> callback) {
delegate.enqueue(new Callback<ResponseBody>() {
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
okhttp3.Response raw = response.raw();
Response<T> converted;
if (raw.isSuccessful()) {
try {
converted =
Response.success(converter.convert(call.request(), response.body()), raw);
} catch (IOException e) {
callback.onFailure(ConverterCall.this, e);
} else {
converted = Response.error(raw.body(), raw);
callback.onResponse(ConverterCall.this, converted);
@Override public void onFailure(Call<ResponseBody> call, Throwable t) {
callback.onFailure(ConverterCall.this, t);
@Override public boolean isExecuted() {
return delegate.isExecuted();
@Override public void cancel() {
@Override public boolean isCanceled() {
return delegate.isCanceled();
@Override public Call<T> clone() {
return new ConverterCall<>(delegate.clone(), converter);
@Override public Request request() {
return delegate.request();
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/"))
.addCallAdapterFactory(new ConverterCallAdapterFactory(
new ConverterCallAdapterFactory.ResponseBodyConverter.Factory() {
public ConverterCallAdapterFactory.ResponseBodyConverter<?> responseBodyConverter(
final Type type, Annotation[] annotations, Retrofit retrofit) {
return new ConverterCallAdapterFactory.ResponseBodyConverter<Object>() {
@Override public Object convert(Request request, ResponseBody body)
throws IOException {
return Jsoup.parse(body.string(), request.url().toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment