如何使用RxAndroid和Retrofit 2检索响应体?

前端之家收集整理的这篇文章主要介绍了如何使用RxAndroid和Retrofit 2检索响应体?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Retrofit 2(beta 4),我希望从使用标准的Call响应转向Rx Android Observable响应.我通过Call< List< ExampleObject>>的简单交换成功切换了我的大多数调用.到Observable< List< ExampleObject>>.我的一些电话使用Call< okhttp3.ResponseBody>,这很好用,但当我换掉Call时,我遇到了一个错误
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<okhttp3.ResponseBody>
            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<okhttp3.ResponseBody>.
         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

之所以我像往常一样使用ResponseBody而不是另一个对象,是因为在这些情况下我需要解析HTML,据我所知,HTML解析器没有Retrofit转换器.我知道我可能自己创建一个,但我宁愿不用我需要解析的少量HTML.

我的问题是为什么Retrofit 2 RxJava适配器在Retrofit 2本身没有支持ResponseBody的情况下呢?有没有其他方法可以从Observable获取响应字符串?

我的服务:

public interface AuthenticationService() {

    @GET("cas/login")
    Observable<Response<ResponseBody>> login();
}

相关的改造代码

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);
}

回应尝试:

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

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

新堆栈跟踪:

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<retrofit2.Response<okhttp3.ResponseBody>>
            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<retrofit2.Response<okhttp3.ResponseBody>>.
         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

解决方法

尝试这样的事情:
import okhttp3.ResponseBody;
import retrofit2.Response;

@GET("/whatever")
Observable<Response<ResponseBody>> getWhatever();

编辑:不要忘记你必须为RxJava指定适配器:

new Retrofit.Builder()
            ...
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build()
            .create(Api.class);

猜你在找的Android相关文章