gpt4 book ai didi

java - RxAndroid 和 Retrofit : Unable to create call adapter for io. reactivex.Observable>

转载 作者:搜寻专家 更新时间:2023-10-30 20:54:41 24 4
gpt4 key购买 nike

我正在尝试使用 rxJava、rxAndroid、Retrofit2 和 OkHTTP3 从 URL 端点下载文件。我的代码无法为“Observable >”创建调用适配器。这些方法对我来说是新的,所以我相信我在这里遗漏了一个重要的概念。非常感谢任何方向或观点。

FATAL EXCEPTION: main Process: com.example.khe11e.rxdownloadfile, PID: 14130 java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex.Observable> for method RetrofitInterface.downloadFileByUrlRx at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720) at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:234) at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:160) at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166) at retrofit2.Retrofit$1.invoke(Retrofit.java:145) at java.lang.reflect.Proxy.invoke(Proxy.java:393) at $Proxy0.downloadFileByUrlRx(Unknown Source) at com.example.khe11e.rxdownloadfile.MainActivity.downloadImage(MainActivity.java:46) at com.example.khe11e.rxdownloadfile.MainActivity$1.onClick(MainActivity.java:39) at android.view.View.performClick(View.java:5207) at android.view.View$PerformClick.run(View.java:21168) at android.os.Handler.handleCallback(Handler.java:746) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5491) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for io.reactivex.Observable>. Tried: * retrofit2.adapter.rxjava.RxJavaCallAdapterFactory * retrofit2.ExecutorCallAdapterFactory at retrofit2.Retrofit.nextCallAdapter(Retrofit.java:237) at retrofit2.Retrofit.callAdapter(Retrofit.java:201) at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:232) ... 16 more

build.gradle:

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.0.4'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

RetrofitInterface.java:

package com.example.khe11e.rxdownloadfile;
import io.reactivex.Observable;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.GET;
import retrofit2.http.Streaming;
import retrofit2.http.Url;

public interface RetrofitInterface {
// Retrofit 2 GET request for rxjava
@Streaming
@GET
Observable<Response<ResponseBody>> downloadFileByUrlRx(@Url String fileUrl);
}

主要 Activity .java:

package com.example.khe11e.rxdownloadfile;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.File;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okio.BufferedSink;
import okio.Okio;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

public class MainActivity extends AppCompatActivity {

Button downloadImgBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadImgBtn = (Button) findViewById(R.id.downloadImgBtn);
downloadImgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadImage();
}
});
}

public void downloadImage(){
RetrofitInterface downloadService = createService(RetrofitInterface.class, "https://www.nasa.gov/");
downloadService.downloadFileByUrlRx("sites/default/files/iss_1.jpg")
.flatMap(processResponse())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(handleResult());
}

public <T> T createService(Class<T> serviceClass, String baseUrl){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(new OkHttpClient.Builder().build())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
return retrofit.create(serviceClass);
}

public Function<Response<ResponseBody>, Observable<File>> processResponse(){
return new Function<Response<ResponseBody>, Observable<File>>() {
@Override
public Observable<File> apply(Response<ResponseBody> responseBodyResponse) throws Exception {
return saveToDiskRx(responseBodyResponse);
}
};
}

private Observable<File> saveToDiskRx(final Response<ResponseBody> response){
return Observable.create(new ObservableOnSubscribe<File>() {
@Override
public void subscribe(ObservableEmitter<File> subscriber) throws Exception {
String header = response.headers().get("Content-Disposition");
String filename = header.replace("attachment; filename=", "");
new File("/data/data/" + getPackageName() + "/images").mkdir();
File destinationFile = new File("/data/data/" + getPackageName() + "/images/" + filename);

BufferedSink bufferedSink = Okio.buffer(Okio.sink(destinationFile));
bufferedSink.writeAll(response.body().source());
bufferedSink.close();

subscriber.onNext(destinationFile);
subscriber.onComplete();
}
});
}

private Observer<File> handleResult(){
return new Observer<File>() {
@Override
public void onSubscribe(Disposable d) {
Log.d("OnSubscribe", "OnSubscribe");
}

@Override
public void onNext(File file) {
Log.d("OnNext", "File downloaded to " + file.getAbsolutePath());
}

@Override
public void onError(Throwable e) {
e.printStackTrace();
Log.d("Error", "Error " + e.getMessage());
}

@Override
public void onComplete() {
Log.d("OnComplete", "onCompleted");
}
};
}
}

我已经尝试按照提到的方式添加调用 here所以它看起来像:

Call<Observable<Response<ResponseBody>>> downloadFileByUrlRx(@Url String fileUrl);

然而,这会导致 flatMap 函数出现问题,因为它找不到符号方法 flatMap(Function< Response< ResponseBody>,Observable< File>>)。

最佳答案

您正在使用 RxJava1 适配器进行改造,将其替换为 RxJava2 变体:

//compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

更新

从 Retrofit 版本 2.2.0 开始,有一个用于 RxJava2 的第一方调用适配器:

compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'

关于java - RxAndroid 和 Retrofit : Unable to create call adapter for io. reactivex.Observable<retrofit2.Response<okhttp3.ResponseBody>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42048520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com