0

I'm trying to get the data I parse from onResponse method via Callback. Here is my ApiClient:

public class ApiClient implements Callback<Map<String, Channel>> {


    private ChannelCallback listener;

    static final String BASE_URL = "https://www.radyoodtu.com.tr/";

    public void start(ChannelCallback listener) {
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        RestInterface restInterface = retrofit.create(RestInterface.class);

        Call<Map<String, Channel>> call = restInterface.getChannels();
        call.enqueue(this);

    }

    @Override
    public void onResponse(retrofit2.Call<Map<String, Channel>> call, Response<Map<String, Channel>> response) {
        System.out.println(response.code());
        if(response.isSuccessful()) {
            Map<String, Channel> body = response.body();
            listener.setChannels(body);
            List<Channel> channels = new ArrayList<>(body.values());
            for (Channel channel : body.values()) {
                System.out.println(channel.getSong());
            }
        }

    }

    @Override
    public void onFailure(retrofit2.Call<Map<String, Channel>> call, Throwable t) {
        //TODO
    }
}

and this is the class I'm trying to get the data:

public class Radio implements ChannelCallback {

    private ApiClient apiClient = new ApiClient();


    public Radio(){
        apiClient.start(this);
    }


    @Override
    public void setChannels(Map<String, Channel> body) {
        this.apiClient.onResponse(body); // NOT WORKING
    }
}

here is my Interface:

public interface ChannelCallback {

    void setChannels(Map<String, Channel> body);
}

what I need to do is get the onResponse body data for Radio class I'm using right now. In Radio class I have to create a List of channel objects with that data I need but I can't even get the data so I can't even create that list. I don't know how to manipulate the data from listener at all and I don't know how can I access that listener I use in ApiClient in Radio class.

cs.kkeser
  • 5
  • 4
  • [This post on 'How can I return value from onResponse of Retrofit v2'](https://stackoverflow.com/a/44881355/5015207) may be helpful – Bö macht Blau Dec 20 '18 at 20:23

2 Answers2

0

It looks like you've got a cyclic reference here. Radio calls ApiClient start, which triggers the network request, which calls Radio.setChannels, which tries to call the client again.

I think you need to resolve two things:

  1. Avoid this cyclic reference
  2. You pass the listener to ApiClient.start() but you never assign it to the actual value inside ApiClient. So, my guess is that you get an NPE here if you have a successful response.
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
0
//i think no need to impliments ChannelCallback if this code works
//just asign your interface and apiclient in class
public class Radio implements ChannelCallback {
private ApiClient apiClient = new ApiClient();
private ChannelCallback channelCallback;

and in inside radio()
public Radio(){
apiClient.start(this);
channelCallback = apiClient.onResponse(body).create(ChannelCallback.class);
channelCallback.setChannels(Map<String, Channel> body)
}

//and recieve callback 
}
Amit
  • 72
  • 4