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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I am trying to use Retrofit and RxJava to make an API call within a custom view in an app that I am working on, but I encounter an incompatible type error when trying to subscribe to the Observable from my Retrofit API call.




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() {

}
});









share|improve this question































    -1















    I am trying to use Retrofit and RxJava to make an API call within a custom view in an app that I am working on, but I encounter an incompatible type error when trying to subscribe to the Observable from my Retrofit API call.




    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() {

    }
    });









    share|improve this question



























      -1












      -1








      -1








      I am trying to use Retrofit and RxJava to make an API call within a custom view in an app that I am working on, but I encounter an incompatible type error when trying to subscribe to the Observable from my Retrofit API call.




      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() {

      }
      });









      share|improve this question
















      I am trying to use Retrofit and RxJava to make an API call within a custom view in an app that I am working on, but I encounter an incompatible type error when trying to subscribe to the Observable from my Retrofit API call.




      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() {

      }
      });






      java android rx-java2 rx-android






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 12:18









      Milad Bahmanabadi

      868718




      868718










      asked Nov 24 '18 at 11:31









      Mohammad Sultan Al NahiyanMohammad Sultan Al Nahiyan

      567




      567
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Use subscribeWith which returns Observer that you passed onto that method. You are probably using old RxJava 1 code. RxJava 2 API changed a bit. Observable now accepts Observer and you should change Subscription to Observer






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457678%2frxjava-subscription-inconvertible-types-cannot-cast-void-to-org-reactivestre%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Use subscribeWith which returns Observer that you passed onto that method. You are probably using old RxJava 1 code. RxJava 2 API changed a bit. Observable now accepts Observer and you should change Subscription to Observer






            share|improve this answer




























              1














              Use subscribeWith which returns Observer that you passed onto that method. You are probably using old RxJava 1 code. RxJava 2 API changed a bit. Observable now accepts Observer and you should change Subscription to Observer






              share|improve this answer


























                1












                1








                1







                Use subscribeWith which returns Observer that you passed onto that method. You are probably using old RxJava 1 code. RxJava 2 API changed a bit. Observable now accepts Observer and you should change Subscription to Observer






                share|improve this answer













                Use subscribeWith which returns Observer that you passed onto that method. You are probably using old RxJava 1 code. RxJava 2 API changed a bit. Observable now accepts Observer and you should change Subscription to Observer







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 13:59









                TubyTuby

                1,85421022




                1,85421022
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457678%2frxjava-subscription-inconvertible-types-cannot-cast-void-to-org-reactivestre%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini