我正在使用 rx
用于连接到 service
与 retrofit
, 下面是 RetrofitApi.java
:
public class RetrofitApi {
private static PublicApi retrofit = null;
public static PublicApi getClient(String url) {
retrofit = new Retrofit.Builder()
.baseUrl(url)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build().create(PublicApi.class);
return retrofit;
}
}
这里是 PublicApi.java
:
public interface PublicApi {
@GET("/web_service/mobile/rest")
Observable<LastNews> lastNews(@Query("function") String function);
}
下面我正在连接到我的服务:
@Override
public void fetchLastNewsStartPage(RemoteDataSource.ResultListener<List<LastNews>> resultListener) {
PublicApi publicApi = RetrofitApi.getClient("https://xxx.xxx.xxx/web_service/");
CompositeDisposable mCompositeDisposable = new CompositeDisposable();
mCompositeDisposable.add(publicApi.lastNews("getLastNews")
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(this::handleResponse, this::handleError));
}
我的问题就在这里,如何将参数发送到handleResponse
和 handleError
.我需要发送这个 RemoteDataSource.ResultListener<List<LastNews>> resultListener
至 handleResponse
和 handleError
:
private void handleResponse(LastNews lastNewses) {
}
private void handleError(Throwable error) {
}
最佳答案
只是不要使用方法引用,因为它只能接受一个参数。您可以使用 lambda 表达式实现结果。而不是
this::handleResponse
写
lastNews -> handleResponse(lastNews, resultListener)
关于java - 将参数发送到订阅 CompositeDisposable 的方法 - Android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45056458/