trocchietto trocchietto - 3 months ago 25
Android Question

RXJava with Retrofit2 I can't retrieve the server response, nor a simple Log.d

I see from the debug that the array is retrieved, but if I put in onNext a Log.d with the first position of the array, I do not retrieve the Log in the console, and I do not know if I am calling the first position of the array in a correct way


Log.d("IVO", "onNext" + stackOverflowQuestions.items.get(0).title.toString());


this is the Main

package com.vogella.android.retrofitstackoverflow;

import (..)


public class MainActivity extends ListActivity {
//original https://api.stackexchange.com/2.2/search?order=desc&sort=activity&tagged=android&site=stackoverflow

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
requestWindowFeature(Window.FEATURE_PROGRESS);
ArrayAdapter<Question> arrayAdapter =
new ArrayAdapter<Question>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
new ArrayList<Question>());
setListAdapter(arrayAdapter);
setProgressBarIndeterminateVisibility(true);
setProgressBarVisibility(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
setProgressBarIndeterminateVisibility(true);
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.stackexchange.com")
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

// prepare call in Retrofit 2.0
StackOverflowAPI stackOverflowAPI = retrofit.create(StackOverflowAPI.class);


//the real call to the server
//Call<StackOverflowQuestions> call = stackOverflowAPI.loadQuestions("android");


Observable<StackOverflowQuestions> observable = stackOverflowAPI.loadQuestions("android");

observable.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<StackOverflowQuestions>() {
@Override
public void onCompleted() {
Log.d("IVO", "completed");
}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(StackOverflowQuestions stackOverflowQuestions) {

Log.d("IVO", "onNext" + stackOverflowQuestions.items.get(0).title.toString());
Log.d("IVO", "onNext" );
// ArrayAdapter<Question> adapter = (ArrayAdapter<Question>) getListAdapter();
// adapter.clear();
// adapter.addAll(response.body().items);
}
});





return true;
}


}

this is StackOverflowQuestions

package com.vogella.android.retrofitstackoverflow;

import java.util.List;

public class StackOverflowQuestions {
List<Question> items;
}


this is Question

package com.vogella.android.retrofitstackoverflow;

// This is used to map the JSON keys to the object by GSON
public class Question {

String title;
String link;

@Override
public String toString() {
return(title);
}
}


EDIT
StackOverflowAPI as requested:

package com.vogella.android.retrofitstackoverflow;

import android.util.Log;

import retrofit2.Callback;
import retrofit2.http.GET;
import retrofit2.http.Query;
import retrofit2.Call;
import rx.Observable;

public interface StackOverflowAPI {
@GET("/2.2/questions?order=desc&sort=creation&site=stackoverflow")
//Call<StackOverflowQuestions> loadQuestions(@Query("tagged") String tags);
Observable<StackOverflowQuestions> loadQuestions(@Query("tagged") String tags);



}

Answer

Finally I found the solution putting this code in onOptionsItemSelected, that is basically where i fetch the content, I receive the response in onNext method of the the Observable, without any need to use the Retrofit2 method onResponse:

adapter.addAll(stackOverflowQuestions.items);

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    setProgressBarIndeterminateVisibility(true);
    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .create();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.stackexchange.com")
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    // prepare call in Retrofit 2.0
    StackOverflowAPI stackOverflowAPI = retrofit.create(StackOverflowAPI.class);


    //the real call to the server
    //Call<StackOverflowQuestions> call = stackOverflowAPI.loadQuestions("android");


    Observable<StackOverflowQuestions> observable = stackOverflowAPI.loadQuestions("android");

    observable.observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new Subscriber<StackOverflowQuestions>() {
                @Override
                public void onCompleted() {
                    Log.d("IVO", "completed");
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(StackOverflowQuestions stackOverflowQuestions) {

                    Log.d("IVO", "onNext " + stackOverflowQuestions.items.get(0).title.toString());
                    Log.d("IVO", "onNext " + stackOverflowQuestions.items.get(1).title.toString());
                    Log.d("IVO", "onNext " + stackOverflowQuestions.items.get(2).title.toString());

                    Log.d("IVO", "onNext");
              ArrayAdapter<Question> adapter = (ArrayAdapter<Question>) getListAdapter();

                    adapter.clear();
                    //setListAdapter(arrayAdapter);

                  //  adapter.addAll(response.body().itemsitems);
                    adapter.addAll(stackOverflowQuestions.items);

                }
            });


    return true;
}
}