androidandroid-studiorx-javaandroidxandroid-mvp

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


I have some classes as presenter and in these classes I use retrofit for some methods. But some methods are duplicated. So I want to use a class for all retrofit and connect to server methods and call them when I want. But when I created that class it has NullpointerException Error

I will be very thankful if you help me

this is presenter codes:

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);
        }
    }

}

And this is a class that I created for get data from server with retrofit and 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;
        }
    }

Solution

  • this method always 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) make this method void

    2) create an interface and call it in onSuccess() and onError()

    3) implement interface in your presenter