0

我正在使用 MVVM 架构,我有我的模型类“Category”和我的 ViewModel 类以及 Recyclerview 的 MainActivity 和适配器,如果我在活动中设置适配器(在改造调用的 onResponse 方法内),一切都很好,但如果我这样做,我不尊重 MVVM 架构的分离,这是用于执行调用的方法:

 public List<Category> getcategories() {

    RestInterface restInterface = rest.getInterfaceService();
    Call<List<Category>> productService = restInterface.getCategories();
    productService.enqueue(new Callback<List<Category>>() {
        @Override
        public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
            if (response.body() == null){
                Log.e(TAG, "responce Call response is null ");
            }else{
                Log.e(TAG, "repo : "+response.body().toString());
                categories = (ArrayList<Category>) response.body();

            }
        }

        @Override
        public void onFailure(Call<List<Category>> call, Throwable t) {
            Log.e(TAG, "Call Fail  : " + t.getMessage());
        }
    });
    Log.e(TAG, "repo 2: "+categories.toString());
    return categories;
}

这是 logcat 结果:

07-11 20:18:34.325 24369-24369/com.instadom E/DataRepository: repo 2: []

07-11 20:18:35.399 24369-24369/com.instadom E/DataRepository: repo : [exemple.com.models.Category@1df175e, exemple.com.models.Category@5cfc73f, exemple.com.models.Category@ 1e7380c, exemple.com.models.Category@7ceb555, exemple.com.models.Category@3014b6a, exemple.com.models.Category@a83985b, exemple.com.models.Category@3d5c8f8, exemple.com.models.Category@ d1251d1]

我无法理解的是为什么我在“Log.e(TAG,”repo 2:“+categories.toString());”中没有得到任何结果 即使“类别”也是一个类对象

我会感谢任何帮助,在此先感谢,


这是代码:

public List getcategories(final Callback> callback) { RestInterface restInterface = rest.getInterfaceService(); 调用> productService = restInterface.getCategories(); productService.enqueue(new retrofit2.Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body() == null){ Log.e(TAG, "responce 呼叫响应是null "); }else{ Log.e(TAG, "repo : "+response.body().toString()); categories = (ArrayList) response.body(); callback.onSuccess(categories); } }

        @Override
        public void onFailure(Call<List<Category>> call, Throwable t) {
            Log.e(TAG, "Call Fail  : " + t.getMessage());
            callback.onFailure(t.getMessage());
        }
    });
    Log.e(TAG, "result : "+categories.toString());
    return categories;
}
public interface Callback<T> {
    void onSuccess(ArrayList<Category> data);
    void onFailure(String reason);
}

这是错误:

java.lang.NullPointerException:尝试在空对象引用上调用接口方法“void instadom.com.repositories.DataRepository$Callback.onSuccess(java.util.ArrayList)”

4

1 回答 1

2

那是因为productService.enqueue是异步调用,该语句Log.e(TAG, "repo 2: "+categories.toString());将在调用入队后立即执行,而onResponseoronFailure将在网络调用后执行。将回调传递给getCategories()以获取这样的类别列表

public interface Callback<List<Categories>> callback{
   void onSuccess(List<Categories> data);
   void onFailure(String reason);
}

或者您可以使用通用回调接口在您的所有网络请求中使用,像这样

public interface Callback<T> callback{
   void onSuccess(List<T> data);
   void onFailure(String reason);
}

然后实现回调功能

public List<Category> getcategories(Callback<List<Category>> callback) {

    RestInterface restInterface = rest.getInterfaceService();
    Call<List<Category>> productService = restInterface.getCategories();
    productService.enqueue(new Callback<List<Category>>() {
        @Override
        public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
            if (response.body() == null){
                Log.e(TAG, "responce Call response is null ");
            }else{
                Log.e(TAG, "repo : "+response.body().toString());
                categories = (ArrayList<Category>) response.body();
                callback.onSuccess(categories);

            }
        }

        @Override
        public void onFailure(Call<List<Category>> call, Throwable t) {
            Log.e(TAG, "Call Fail  : " + t.getMessage());
            callback.onError(t.getMessage());
        }
    });
}
于 2019-07-11T19:57:35.617 回答