我正在使用Retrofit 2(測試版4),並且我正在尋求從使用標準Call
響應到RxAndroid Observable
響應。我通過從Call<List<ExampleObject>>
到Observable<List<ExampleObject>>
的簡單交換成功地切換了我的大部分電話。我的一些電話的使用Call<okhttp3.ResponseBody>
,偉大的工程,但是當我換出Call
,我遇到了一個錯誤:如何用RxAndroid和Retrofit 2檢索響應體?
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。
我的問題是爲什麼改造2 RxJava適配器支持ResponseBody
當改造2本身呢?有沒有另一種方法可以從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
請張貼實際的代碼。 –
@TudorLuca更新了代碼 – Bryan
@TudorLuca我很抱歉浪費你的時間。我發現我實際上使用了一個沒有RxJavaCallAdapterFactory的Retrofit對象(我應該刪除它)。我撕開了當前的代碼,這有點混亂。無論如何,謝謝你的幫助。 – Bryan