简体   繁体   English

Android Callback方法未填充列表

[英]Android Callback method not fills the list

I'm working on an Android Project right now and I'm trying to parse from an URL. 我现在正在开发一个Android项目,并且正在尝试从URL进行解析。 In my "ApiClient" I have no problem to parse. 在我的“ ApiClient”中,我没有问题可以解析。 Here is my "ApiClient" class: 这是我的“ ApiClient”类:

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

    static final String BASE_URL = "someURL";

    public void start() {

        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();
            List<Channel> channels = new ArrayList<>(body.values());
        }
...
    }

I'm trying to get the response into a List from using callback in my "Radio" class. 我正在尝试通过在“无线电”类中使用回调将响应添加到列表中。 This the place where I'm having the problem. 这是我遇到问题的地方。 I tried this three too but it didn't solved my problem: 我也尝试了这三个方法,但没有解决我的问题:

private List<Channel> listChannels = new ArrayList<Channel>();
private List<Channel> listChannels = new ArrayList<>();
private List<Channel> listChannels = new List<>();

Here is my "Radio" class: 这是我的“无线电”课程:

public class Radio {
    private static final String STORAGE = "radio";
    private List<Channel> listChannels;

    public static Radio get() {
        return new Radio();
    }

    private SharedPreferences storage;

    private Radio() {
        storage = App.getInstance().getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
    }

    public List<Channel> getData() {
        RestInterface restInterface = SingletonClass.getService();
        restInterface.getChannels().enqueue(new Callback<Map<String, Channel>>() {
            @Override
            public void onResponse(Call<Map<String, Channel>> call, Response<Map<String, Channel>> response) {
                if(response.isSuccessful()){
                    Map<String, Channel> body = response.body();
                    List<Channel> channels = new ArrayList<>(body.values());
                    loadChannels(channels);
                }
            }

            @Override
            public void onFailure(Call<Map<String, Channel>> call, Throwable t) {

            }
        });
        System.out.println(listChannels.get(1).getArtist());
        return listChannels;
    }

    public boolean isRated(int itemId) {
        return storage.getBoolean(String.valueOf(itemId), false);
    }

    public void setRated(int itemId, boolean isRated) {
        storage.edit().putBoolean(String.valueOf(itemId), isRated).apply();
    }

    private void loadChannels(List<Channel> channels){
        listChannels.clear();
        listChannels.addAll(channels);
    }


}

Here is my interface class: 这是我的接口类:

public interface RestInterface {

    @GET("someURL")
    retrofit2.Call<Map<String, Channel>> getChannels();
}

and my SingletonClass: 和我的SingletonClass:

public class SingletonClass{

    private static final Retrofit RETROFIT = new Retrofit.Builder()
            .baseUrl(someURL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    private static final RestInterface SERVICE = RETROFIT.create(RestInterface.class);


    public static RestInterface getService(){
        return SERVICE;
    }

}

I don't know what should I do to fill the List in my Radio class now. 我不知道现在该怎么办才能在电台课程中填写列表。 I'm totally open to suggestions. 我完全愿意提出建议。 Thanks for the help. 谢谢您的帮助。

Are you getting an empty list? 您有空名单吗? You're asynchronously setting in the channel data in getData(), so if you're trying to get the data by reading it in the next line, it may not be loaded yet. 您正在getData()中异步设置通道数据,因此,如果您试图通过在下一行中读取数据来获取数据,则可能尚未加载。

This means that when you call System.out.println(listChannels.get(1).getArtist()), you won't see the result of loadChannels, because that call happens right after you call enqueue() while loadChannels() is running on a separate thread. 这意味着当您调用System.out.println(listChannels.get(1).getArtist()),您将看不到System.out.println(listChannels.get(1).getArtist()),的结果,因为该调用是在您调用enqueue()之后立即发生的,而loadChannels()为在单独的线程上运行。 If you moved that into onResponse() you might have more luck. 如果将其移到onResponse()中,可能会更幸运。

In Android, a fairly easy way to do things like this and interact with the UI is by using AsyncTask , which for you would look something like this: 在Android中,执行此类操作并与UI交互的一种相当简单的方法是使用AsyncTask ,对于您来说,它看起来像这样:

private class loadChannelTask extends AsyncTask<Void, Void, List<Channel>> {
    protected List<Channel> doInBackground() {
        //get response
        //pass to load channels
    }

    protected void onPostExecute() {
         System.out.println(listChannels.get(1).getArtist()); //presumably the artist name
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM