How Retrieve Response Body with RxAndroid and Retrofit 2?
<p>I am using Retrofit 2 (beta 4), and I was looking to move from using the standard <code>Call</code> response to the RxAndroid <code>Observable</code> response. I was successful in switching most of my calls with a simple swap from <code>Call&lt;List&lt;ExampleObject&gt;&gt;</code> to <code>Observable&lt;List&lt;ExampleObject&gt;&gt;</code>. A few of my calls use <code>Call&lt;okhttp3.ResponseBody&gt;</code>, which works great, but when I swapped out <code>Call</code>, I was met with an error:</p>

<pre><code>03-03 15:21:44.237 27333-27333/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.app, PID: 27333
        java.lang.IllegalArgumentException: Unable to create call adapter for rx.Observable&lt;okhttp3.ResponseBody&gt;
            for method AuthenticationService.getLoginForm
            at retrofit2.Utils.methodError(Utils.java:119)
            at retrofit2.MethodHandler.createCallAdapter(MethodHandler.java:52)
            at retrofit2.MethodHandler.create(MethodHandler.java:25)
            at retrofit2.Retrofit.loadMethodHandler(Retrofit.java:164)
            at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
            at java.lang.reflect.Proxy.invoke(Proxy.java:393)
            at $Proxy6.getLoginForm(Unknown Source)
            at com.example.app.ui.fragment.LoginFragment.login(LoginFragment.java:214)
            at com.example.app.ui.fragment.LoginFragment.lambda$onContinue$1(LoginFragment.java:168)
            at com.example.app.ui.fragment.LoginFragment.access$lambda$1(LoginFragment.java)
            at com.example.app.ui.fragment.LoginFragment$$Lambda$4.onClick(Unknown Source)
            at android.view.View.performClick(View.java:5204)
            at android.view.View$PerformClick.run(View.java:21153)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for rx.Observable&lt;okhttp3.ResponseBody&gt;.
         Tried:
          * retrofit2.ExecutorCallAdapterFactory
           at retrofit2.Retrofit.nextCallAdapter(Retrofit.java:230)
           at retrofit2.Retrofit.callAdapter(Retrofit.java:194)
           at retrofit2.MethodHandler.createCallAdapter(MethodHandler.java:50)
              ... 18 more
</code></pre>

<p>The reason I am using the <code>ResponseBody</code> instead of another object as usual, is because in these cases I need to parse HTML, and as far as I know, there is no Retrofit converter for an HTML parser. I know I probably could create one on my own, but I would rather not for the small amount of HTML I have to parse.</p>

<p>My question is why doesn't the Retrofit 2 RxJava Adapter support <code>ResponseBody</code> when Retrofit 2 itself does? Is there another way I can obtain the response string from an <code>Observable</code>? </p>

<hr>

<p>My Service:</p>

<pre><code>public interface AuthenticationService() {

    @GET("cas/login")
    Observable&lt;Response&lt;ResponseBody&gt;&gt; login();
}
</code></pre>

<p>Relevant Retrofit code:</p>

<pre><code>public static Retrofit getRetrofit() {
    if(mRetrofit == null) {
        return new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(getGson()))
                .client(getOkHttpClient())
                .build();
    } return mRetrofit;
}

public static AuthenticationService getAuthenticationService() {
    return getRetrofit().create(AuthenticationService.class);
}
</code></pre>

<p>Response attempt:</p>

<pre><code>private void login() {
    RestClient.getAuthenticationService().login()
            .observeOn(ASchedulers.newThread())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnNext(this::onLoginResponse);
}

private void onLoginResponse(Response&lt;ResponseBody&gt;&gt; response) {
    try {
        parseResponse(response.body().string());
    } catch (IOException) {
        Timber.w(throwable, "Failed to login");
    }
}
</code></pre>

<p>New stack trace:</p>

<pre><code>03-03 16:14:57.848 26866-26866/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.app, PID: 26866
        java.lang.IllegalArgumentException: Unable to create call adapter for rx.Observable&lt;retrofit2.Response&lt;okhttp3.ResponseBody&gt;&gt;
            for method AuthenticationService.getLoginForm
            at retrofit2.Utils.methodError(Utils.java:119)
            at retrofit2.MethodHandler.createCallAdapter(MethodHandler.java:52)
            at retrofit2.MethodHandler.create(MethodHandler.java:25)
            at retrofit2.Retrofit.loadMethodHandler(Retrofit.java:164)
            at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
            at java.lang.reflect.Proxy.invoke(Proxy.java:393)
            at $Proxy3.getLoginForm(Unknown Source)
            at com.example.app.ui.fragment.LoginFragment.login(LoginFragment.java:206)
            at com.example.app.ui.fragment.LoginFragment.lambda$onContinue$1(LoginFragment.java:160)
            at com.example.app.ui.fragment.LoginFragment.access$lambda$1(LoginFragment.java)
            at com.example.app.ui.fragment.LoginFragment$$Lambda$4.onClick(Unknown Source)
            at android.view.View.performClick(View.java:5204)
            at android.view.View$PerformClick.run(View.java:21153)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for rx.Observable&lt;retrofit2.Response&lt;okhttp3.ResponseBody&gt;&gt;.
         Tried:
          * retrofit2.ExecutorCallAdapterFactory
           at retrofit2.Retrofit.nextCallAdapter(Retrofit.java:230)
           at retrofit2.Retrofit.callAdapter(Retrofit.java:194)
           at retrofit2.MethodHandler.createCallAdapter(MethodHandler.java:50)
              ... 18 more
</code></pre>