RXJava 订阅不可转换类型;无法将 'void' 转换为 'org.reactivestreams.Subscription?

RXJava Subscription Inconvertible types; cannot cast 'void' to 'org.reactivestreams.Subscription?

我正在尝试使用 Retrofit 和 RxJava 在我正在开发的应用程序的自定义视图中进行 API 调用,但是在尝试订阅 Observable 时遇到了不兼容的类型错误我的改造 API 调用。

ApiService

public interface ApiService {
@GET("airline-tickets.php")
Observable<List<Ticket>> getStarredRepositories();
}

ApiClient

public class ApiClient {
private static final String GITHUB_BASE_URL = "https://api.androidhive.info/json/";
private ApiService apiService;
private static ApiClient instance;

public ApiClient() {
    final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(GITHUB_BASE_URL)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    apiService = retrofit.create(ApiService.class);
}

public static ApiClient getInstance() {
    if (instance == null) {
        instance = new ApiClient();
    }
    return instance;
}

public Observable<List<Ticket>> getStarredRepos() {
    return apiService.getStarredRepositories();
}
}

MainActivity

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Subscription subscription= (Subscription) ApiClient.getInstance()
            .getStarredRepos()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<Ticket>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(List<Ticket> tickets) {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });
}
}

This part of code showing Inconvertible types; cannot cast 'void' to 'org.reactivestreams.Subscription

Subscription subscription= (Subscription) ApiClient.getInstance()
            .getStarredRepos()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<Ticket>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(List<Ticket> tickets) {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });

使用您传递给该方法的 subscribeWith returns Observer。您可能正在使用旧的 RxJava 1 代码。 RxJava 2 API 改变了一点。 Observable 现在接受 Observer,您应该将 Subscription 更改为 Observer