Rosenberg
Rosenberg

Reputation: 2444

Why am I getting a null value onNext in RxJava? (rxjava2 + retrofit2)

This is the interface:

@GET("solicitation/get/{protocol}")
Observable<Solicitation> getProtocol(@Path("protocol") String protocol, @Query("X-Authorization") String apiKey);

And this is in my ApiClient class,

    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(httpClient.build())
                .build();

This is how I call it on my MainActivity onCreate:

    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

    apiService.getProtocol("2313868283", getResources().getString(R.string.api_key))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(obSol);

And this is outside of onCreate:

Observer<Solicitation> obSol  = new Observer<Solicitation>() {

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        Log.e(TAG, "onSubscribe" + Thread.currentThread().getName());
    }

    @Override
    public void onNext(@NonNull Solicitation solicitation) {
        Log.e(TAG, "onNext: " + solicitation.getName() + " " + Thread.currentThread().getName());
    }

    @Override
    public void onError(@NonNull Throwable e) {
        Log.e(TAG, "onError: " + e.toString());
    }

    @Override
    public void onComplete() {
        Log.e(TAG, "onComplete: All Done!" + Thread.currentThread().getName());
    }
};

When I run the app retrofit does its magic, yanks the results out of that api call and everything, except I get null where I should've gotten the actual result:

        06-11 12:26:31.691 22760-22760/com.example.ga.myapplication E/lol: onSubscribemain
        06-11 12:26:31.704 22760-22798/com.example.ga.myapplication D/OkHttp: --> GET (URL HIDDEN FOR THIS POST) http/1.1
        06-11 12:26:31.704 22760-22798/com.example.ga.myapplication D/OkHttp: --> END GET
        06-11 12:26:31.814 22760-22801/com.example.ga.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
        06-11 12:26:31.814 22760-22801/com.example.ga.myapplication D/OpenGLRenderer: Swap behavior 1
        06-11 12:26:31.814 22760-22801/com.example.ga.myapplication W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
        06-11 12:26:31.814 22760-22801/com.example.ga.myapplication D/OpenGLRenderer: Swap behavior 0
        06-11 12:26:32.054 22760-22760/com.example.ga.myapplication W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
        06-11 12:26:32.511 22760-22798/com.example.ga.myapplication D/OkHttp: <-- 200 OK (URL HIDDEN FOR THIS POST) (807ms)
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Date: Sun, 11 Jun 2017 15:40:51 GMT
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Server: Apache/2.4.18 (Ubuntu)
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Access-Control-Allow-Origin: *
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Access-Control-Allow-Methods: GET, POST, OPTIONS
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Access-Control-Allow-Credentials: true
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Cache-Control: no-cache
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Set-Cookie: XSRF-TOKEN=ADA1ODc0MWRlNWZlOWM0MjJhYWNkMTc1OGZjMzk2ZDhlOGJhNTZlZDRhM2RmN2ZjMzk0NjFmOTdmIn0%3D; expires=Sun, 11-Jun-2017 17:40:51 GMT; Max-Age=7200; path=/
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Set-Cookie: laravel_session=eiUADHiuhsaduihAIUSDVuRWpacDR4Mm5wSzljNnc0YzJ5dFBBQ2NDU5ZTFmYjg3ZmM3ZiJ9; expires=Sun, 11-Jun-2017 17:40:51 GMT; Max-Age=7200; path=/; HttpOnly
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Content-Length: 329
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Keep-Alive: timeout=5, max=100
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Connection: Keep-Alive
        06-11 12:26:32.512 22760-22798/com.example.ga.myapplication D/OkHttp: Content-Type: application/json
        06-11 12:26:32.514 22760-22798/com.example.ga.myapplication D/OkHttp: {"data":[{"id":5,"name":"Roll D","protocol":"2313868283"}]}
        06-11 12:26:32.514 22760-22798/com.example.ga.myapplication D/OkHttp: <-- END HTTP (329-byte body)
        06-11 12:26:32.520 22760-22760/com.example.ga.myapplication E/lol: onNext: null main
        06-11 12:26:32.520 22760-22760/com.example.ga.myapplication E/lol: onComplete: All Done!main

Edit:

To make it a little clearer this is the call onNext:

Log.e(TAG, "onNext: " + solicitation.getName() + " " + Thread.currentThread().getName());

And this is the log:

        06-11 12:26:32.520 22760-22760/com.example.ga.myapplication E/lol: onNext: null main

Upvotes: 0

Views: 596

Answers (1)

Kiskae
Kiskae

Reputation: 25573

It is null because the JSON returned by the webservice cannot be mapped into your Solicitation class.

Looking at the JSON logged you need to replace the Solicitation with the following class:

class ApiResponse {
    List<Solicitation> data;
}

Since the API returns an object that contains a list of Solicitation.

Upvotes: 1

Related Questions