简体   繁体   中英

how to keep the response.body() data of Retrofit onRespose() call in a singleton

I tried to store the data from response.body() during a network call using retrofit. But I couldn't access the data using a singleton object.

Here is my singleton class code:

public class FishCategory {
    private SparseArray<String> sparseArray = new SparseArray<>(3);
    private static FishCategory singleton;

    public static FishCategory getSingleton() {
        if (singleton == null) {
            singleton = new FishCategory();
        }
        return singleton;
    }

    public SparseArray<String> getSparse() {
        ApiService service = ApiClient.getClient().create(ApiService.class);
        Call<CategoryResp> call = service.categoryAPI();
        call.enqueue(new Callback<CategoryResp>() {
            @Override
            public void onResponse(Call<CategoryResp> call, Response<CategoryResp> response) {

                CategoryResp categoryResp = response.body();
                for (int i = 0; i < categoryResp.getsData().getCategoryList().size(); i++) {
                    sparseArray.put(i, categoryResp.getsData().getCategoryList().get(i).getCatTitle());
                }
            }

            @Override
            public void onFailure(Call<CategoryResp> call, Throwable t) {

            }
        });
        return sparseArray;
    }
}

Now if I use this singleton object in a different class, it returns null. PLEASE HELP....

public SparseArray<String> getSparse() {
        ApiService service = ApiClient.getClient().create(ApiService.class);
        Call<CategoryResp> call = service.categoryAPI();
        call.enqueue(new Callback<CategoryResp>() {
            @Override
            public void onResponse(Call<CategoryResp> call, Response<CategoryResp> response) {

                CategoryResp categoryResp = response.body();
                for (int i = 0; i < categoryResp.getsData().getCategoryList().size(); i++) {
                    sparseArray.put(i, categoryResp.getsData().getCategoryList().get(i).getCatTitle());
                }
            }

            @Override
            public void onFailure(Call<CategoryResp> call, Throwable t) {

            }
        });
        return sparseArray;
    }

You are not creating non blocking coding in this case .Using enqueue you are shifting it to a separate thread while your code is still being executed which would obviously not wait for your code to execute.So your sparseArray is null. Please use below for blocking nature and do note that below would be working on Main UI thread which would throw exception.

TaskService taskService = ServiceGenerator.createService(TaskService.class);  
Call<List<Task>> call = taskService.getTasks();  
List<Task>> tasks = call.execute().body();  

Read below for understanding synchronous and asynchronous nature . https://futurestud.io/tutorials/retrofit-synchronous-and-asynchronous-requests

    public SparseArray<String> getSparse() {
            ApiService service = ApiClient.getClient().create(ApiService.class);
            Call<CategoryResp> call = service.categoryAPI();
            call.execute().body();
            for( i = 0; i < categoryResp.getsData().getCategoryList().size(); i++) {
                        sparseArray.put(i, categoryResp.getsData().getCategoryList().get(i).getCatTitle());
            }
            return sparseArray;
        }

我正在使用SharedPreferences以及Retrofit异步调用,问题已解决。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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