Android 改型API调用是在我尝试显示结果后启动的

Android 改型API调用是在我尝试显示结果后启动的,android,retrofit2,Android,Retrofit2,我使用的是MVVM架构,我有我的模型类“Category”和我的ViewModel类,还有用于Recyclerview的MainActivity和适配器, 如果我在活动中设置适配器(在改装调用的onResponse方法中),一切都正常,但是如果我这样做,我不尊重MVVM体系结构的分离, 以下是用于执行调用的方法: public List<Category> getcategories() { RestInterface restInterface = rest.getIn

我使用的是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;
}
public List getcategories(){
RestInterface RestInterface=rest.getInterfaceService();
调用productService=restInterface.getCategories();
productService.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.body()==null){
Log.e(标记“response Call response is null”);
}否则{
Log.e(标签,“repo:+response.body().toString());
categories=(ArrayList)response.body();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(标记“调用失败:+t.getMessage());
}
});
Log.e(标记“repo 2:+categories.toString());
退货类别;
}
以下是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:[example.com.models。Category@1df175e,example.com.models。Category@5cfc73f,example.com.models。Category@1e7380c,example.com.models。Category@7ceb555,example.com.models。Category@3014b6a,example.com.models。Category@a83985b,example.com.models。Category@3d5c8f8,example.com.models。Category@d1251d1]

我不明白的是,为什么我在“Log.e(TAG,repo 2:+categories.toString());“even tho”categories“是一个类对象”中没有得到任何结果

我非常感谢你的帮助, 提前感谢,


代码如下:

公共列表getcategories(最终回调> (回拨){ RestInterface RestInterface=rest.getInterfaceService(); 调用>productService=restInterface.getCategories(); productService.enqueue(新版本2.Callback>(){ @凌驾 公共void onResponse(调用>调用,响应>响应){ if(response.body()==null){ Log.e(标记“response Call response is null”); }否则{ Log.e(标签,“repo:+response.body().toString()); categories=(ArrayList)response.body(); callback.onSuccess(类别); } }

        @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);
}
@覆盖
失败时公共无效(调用调用,可丢弃的t){
Log.e(标记“调用失败:+t.getMessage());
callback.onFailure(t.getMessage());
}
});
Log.e(标记,“结果:+categories.toString());
退货类别;
}
公共接口回调{
成功时无效(ArrayList数据);
失效时无效(字符串原因);
}
以下是错误:


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

,这是因为
productService.enqueue
是一个异步调用,语句
Log.e(标记,“repo 2:+categories.toString());
将在调用结束后立即执行,而
onResponse
onFailure
将在网络调用后执行。将回调传递到
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 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());
        }
    });
}
公共列表getcategories(回调){ RestInterface RestInterface=rest.getInterfaceService(); 调用productService=restInterface.getCategories(); productService.enqueue(新回调(){ @凌驾 公共void onResponse(调用、响应){ if(response.body()==null){ Log.e(标记“response Call response is null”); }否则{ Log.e(标签,“repo:+response.body().toString()); categories=(ArrayList)response.body(); callback.onSuccess(类别); } } @凌驾 失败时公共无效(调用调用,可丢弃的t){ Log.e(标记“调用失败:+t.getMessage()); callback.onError(t.getMessage()); } }); }
这是因为
productService.enqueue
是一个异步调用,语句
Log.e(标记“repo 2:”+categories.toString());
将在调用结束后立即执行,而
onResponse
onFailure
将在网络调用后执行。将回调传递到
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 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());
        }
    });
}
公共列表getcategories(回调){ RestInterface RestInterface=rest.getInterfaceService(); 调用productService=restInterface.getCategories(); productService.enqueue(新回调(){ @凌驾 公共void onResponse(调用、响应){ 如果(答复)