0

I don't have any type of error when I run the application, but the problem is I don't retrieve the data from my interface. I use Dagger to inject dependencies, and retrofit to retrieve the data from Last.fm Api.

When I try to use a log to check if the data is null, the message is not displayed in logcat and don't work with a toast message.

Service interface

public interface GenreTopTracksService {
  @GET("?method=tag.gettoptracks&format=json")
  Single<GenreTopTracksResponse> getGenreTopTracks(@Query("tag") String user, @Query("limit") int limit, @Query("api_key") String apiKey);
}

Presenter interface

public interface GenreTopTracksPresenter {
  void getGenreTopTracks(String tag, int limit, String apiKey);
}

Interactor interface

public interface GenreTopTracksInteractor {
  Single<GenreTopTracksResponse> getGenreTopTracks(String tag, int limit, String apiKey);
}

View interface -- here i have the update data method

public interface GenreTopTracksView {
  void updateData(List<Track> tracks);
}

this class is an a implementation of the GenreTopTrackPresneter

public class GenreTopTracksPrensenterImpl implements GenreTopTracksPresenter {


Disposable mDisposable;
GenreTopTracksInteractor mInteractor;
GenreTopTracksView mGenreViewData;


public GenreTopTracksPrensenterImpl(GenreTopTracksInteractor mInteractor, GenreTopTracksView mGenreViewData) {
    this.mInteractor = mInteractor;
    this.mGenreViewData = mGenreViewData;
}

@Override
public void getGenreTopTracks(String tag, int limit, String apiKey) {
    disposeRequest();
    mDisposable = mInteractor.getGenreTopTracks(tag, limit, apiKey)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .map(new Function<GenreTopTracksResponse, List<Track>>() {
                @Override
                public List<Track> apply(@NonNull GenreTopTracksResponse topTracksResponse) throws Exception {
                    if (topTracksResponse != null && topTracksResponse.getTopTracks() != null && topTracksResponse.getTopTracks().getTracks() != null) {
                        return topTracksResponse.getTopTracks().getTracks();
                    }
                    return new ArrayList<Track>();
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<List<Track>>() {
                @Override
                public void accept(@NonNull List<Track> tracks) throws Exception {

                    if (tracks.size() == 0) {
                        // NO se muestra nada
                    }else{
                     mGenreViewData.updateData(tracks);
                    }

                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(@NonNull Throwable throwable) throws Exception {
                    Log.d("ERRORGENRE", "Errorxd");

                }
            });
}


private void disposeRequest() {
    if (mDisposable != null && !mDisposable.isDisposed()) {
        mDisposable.dispose();
    }
}
}

THis is my module class to inject dependencies to my main activity @Module public class GenreTopTracksModule {

GenreTopTracksView mView;

public GenreTopTracksModule(GenreTopTracksView view) {
    mView = view;

}

// provides the view to create the top tracks presenter
@Singleton
@Provides
public GenreTopTracksView providesTopTracksView() {
    return this.mView;
}

// provides a converter factory to create the retrofit instance
@Singleton
@Provides
public Converter.Factory providesConverterFactory() {
    return GsonConverterFactory.create();
}

// provides a call adapter factory needed to integrate rxjava with retrofit
@Singleton
@Provides
public CallAdapter.Factory providesCallAdapterFactory() {
    return RxJava2CallAdapterFactory.create();
}

// provides a retrofit instance to create the top tracks interactor
@Singleton
@Provides
public Retrofit providesRetrofit(Converter.Factory converter, CallAdapter.Factory adapter) {
    return new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addCallAdapterFactory(adapter)
            .addConverterFactory(converter)
            .build();
}

// provides top tracks interactor to make an instance of the presenter
@Singleton
@Provides
public GenreTopTracksInteractor providesTopTopTracksInteractor(Retrofit retrofit) {
    return new GenreTopTracksInteractorImplementation(retrofit);
}

// provides top track presenter
@Singleton
@Provides
public GenreTopTracksPresenter providesTopTracksPresenter(GenreTopTracksInteractor interactor, GenreTopTracksView mView) {
    return new GenreTopTracksPrensenterImpl(interactor, mView);

}
}

And this is my main activity

public class SelectionActivity extends AppCompatActivity implements GenreTopTracksView{


@Inject
GenreTopTracksPresenter mPresenter;
Button mButton;
EditText mEditText;
String tag;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selection);
    DaggerGenreTopTracksComponent.builder().genreTopTracksModule(new GenreTopTracksModule(this)).build().inject(this);
    mButton = (Button) findViewById(R.id.button_prueba);
    mEditText = (EditText) findViewById(R.id.edit_prueba);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            tag = mEditText.getText().toString();
            mPresenter.getGenreTopTracks(tag, Constants.TOP_ITEMS_LIMIT, Constants.API_KEY);

        }
    });

}


@Override
public void updateData(List<Track> tracks) {

    if(tracks != null){
        for(int x = 0; x<tracks.size(); x++){
            Log.d("datasetTrack", tracks.get(x).getName());
            Toast.makeText(SelectionActivity.this, tracks.get(x).getName(), Toast.LENGTH_SHORT).show();

        }
    }else if(tracks == null){
        Log.d("datasetTrack", "datos nulos :(");
        Toast.makeText(SelectionActivity.this, "Datos nulos", Toast.LENGTH_SHORT).show();
    }

}
}

I need to see the data in the "updateData" method.

Joshua de Guzman
  • 2,063
  • 9
  • 24
MADMVX
  • 354
  • 1
  • 4
  • 18
  • you never call `.enqueue()` and these data-types do not seem to match. – Martin Zeitler Jan 25 '19 at 02:24
  • @MartinZeitler, he does not need that, he used retrofit + rxjava2 setup + included some factory already. – Joshua de Guzman Jan 25 '19 at 02:53
  • @joshuadeguzman the retrofit does not request... and the question is generally missing the POJO. – Martin Zeitler Jan 25 '19 at 02:54
  • Hi @MartinZeitler, I think you could take a look at this https://square.github.io/retrofit/2.x/retrofit/retrofit2/Call.html#enqueue-retrofit2.Callback-, `.enqueue()` is a method available when you use Retrofit's `Call` callback, otherwise if you prefer to use Retrofit with RxJava, you may not need to. Read more on: http://randomdotnext.com/retrofit-rxjava/ – Joshua de Guzman Jan 25 '19 at 03:21
  • @Dskato can you show us how you serialized the response (show us your model object)? – Joshua de Guzman Jan 25 '19 at 03:22
  • @joshuadeguzman I edited some things, and the interface its ok but, when i call this service, https://www.last.fm/api/show/user.getTopTracks work but don't with this https://www.last.fm/api/show/tag.getTopTracks , both uses the same serialized response except the '@Query()', edited for tag but the data is empty – MADMVX Jan 25 '19 at 03:45

1 Answers1

0

First of all recommend you add retrofit client interceptor Add dependency in build.gradle:

compile 'com.squareup.okhttp3:logging-interceptor:$your_version'

in GenreTopTrackModule change method

@Singleton
@Provides
public Retrofit providesRetrofit(Converter.Factory converter, CallAdapter.Factory adapter) {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
    return new Retrofit.Builder()
           .client(client)
           .baseUrl(Constants.BASE_URL)
           .addCallAdapterFactory(adapter)
           .addConverterFactory(converter)
           .build();
}

As a result in Logcat you will see response what last.fm api return. If response return right check you implementation

a23sokolov
  • 603
  • 3
  • 11
  • This is in my module injection, read the comment pls, I think is problem with the webService – MADMVX Jan 25 '19 at 12:18