【问题标题】:Get values with callback method for other class in android retrofit在android改造中使用其他类的回调方法获取值
【发布时间】:2019-05-21 08:20:31
【问题描述】:

我正在尝试通过回调从 onResponse 方法中获取我解析的数据。这是我的 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
    }
}

这是我要获取数据的类:

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
    }
}

这是我的界面:

public interface ChannelCallback {

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

我需要做的是获取我现在正在使用的 Radio 类的 onResponse 正文数据。在 Radio 类中,我必须使用我需要的数据创建一个频道对象列表,但我什至无法获取数据,因此我什至无法创建该列表。我根本不知道如何操作来自侦听器的数据,也不知道如何访问我在 Radio 类的 ApiClient 中使用的那个侦听器。

【问题讨论】:

标签: java android retrofit2 asynccallback


【解决方案1】:
//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 
}

【讨论】:

  • 那么@Override setChannels 发生了什么? 'body' 也无法解析。
【解决方案2】:

看起来你在这里有一个循环引用。 Radio调用ApiClient start,触发网络请求,调用Radio.setChannels,尝试再次调用客户端

我认为你需要解决两件事:

  1. 避免这种循环引用
  2. 您将listener 传递给ApiClient.start(),但从未将其分配给ApiClient 中的实际值。所以,我的猜测是,如果你有一个成功的响应,你会在这里得到一个 NPE。

【讨论】:

    猜你喜欢
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 2011-01-17
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多