我怎样才能在不同的 类 中连接 retrofit 和 RxJava?

How can I make connection between retrofit and RxJava in different classes?

我有一些 classes 作为演示者,在这些 classes 中,我对某些方法使用了改造。但是有些方法是重复的。所以我想对所有改造使用 class 并连接到服务器方法并在需要时调用它们。 但是当我创建那个 class 时它有 NullpointerException Error

如果你能帮助我,我将不胜感激

这是主持人代码:

public class DefinitionPresenter implements DefinitionContract.Presenter {
    private KalaBeanDataSource kalaBeanDataSource;
    private DefinitionContract.View view;
    private CompositeDisposable compositeDisposable = new CompositeDisposable();
    private DatabaseMethods databaseMethods;
    private ActivityKindList activityKindList;

    public DefinitionPresenter(KalaBeanDataSource kalaBeanDataSource){
        this.kalaBeanDataSource = kalaBeanDataSource;
        databaseMethods = new DatabaseMethods(kalaBeanDataSource,compositeDisposable);
        activityKindList = new ActivityKindList();
    }


    @Override
    public void attachView(DefinitionContract.View view) {
        this.view = view;
    }

    @Override
    public void detachView() {
        view = null;
        if(compositeDisposable != null && compositeDisposable.size() > 0){
            compositeDisposable.clear();
        }
    }


    @Override
    public void activityKind() {
        activityKindList = databaseMethods.getActivityKind();
        if(activityKindList.getItems().size() > 0){
            view.getActivityKind(activityKindList);
        }else{
            view.showMessage(databaseMethods.message);
        }
    }

}

这是我创建的 class,用于通过改造和 RxJava 从服务器获取数据

public  class DatabaseMethods {
    private KalaBeanDataSource kalaBeanDataSource;
    private CompositeDisposable compositeDisposable;
    private ActivityKindList activityKindListResult;
    public String message = null;


    public  DatabaseMethods(KalaBeanDataSource kalaBeanDataSource,CompositeDisposable compositeDisposable){
        this.kalaBeanDataSource = kalaBeanDataSource;
        this.compositeDisposable = compositeDisposable;
        activityKindListResult = new ActivityKindList();
    }

    public  ActivityKindList getActivityKind(){
        kalaBeanDataSource.getActivityKind().subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<ActivityKindList>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                            compositeDisposable.add(d);

                    }

                    @Override
                    public void onSuccess(ActivityKindList activityKindList) {
                        activityKindListResult = activityKindList;
                    }

                    @Override
                    public void onError(Throwable e) {
                        message = e.toString();
                    }
                });

        if(message == null && activityKindListResult.getItems().size() > 0){
            return activityKindListResult;
        }else{
            return null;
        }
    }

这个方法总是returns null:

 public  ActivityKindList getActivityKind(){
    kalaBeanDataSource.getActivityKind().subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new SingleObserver<ActivityKindList>() {
                @Override
                public void onSubscribe(Disposable d) {

                        compositeDisposable.add(d);

                }

                @Override
                public void onSuccess(ActivityKindList activityKindList) {
                    activityKindListResult = activityKindList;
                }

                @Override
                public void onError(Throwable e) {
                    message = e.toString();
                }
            });

    if(message == null && activityKindListResult.getItems().size() > 0){
        return activityKindListResult;
    }else{
        return null;
    }
}

1) 使该方法失效

2) 创建一个接口并在onSuccess()onError()

中调用它

3) 在演示者中实现界面