代码之家  ›  专栏  ›  技术社区  ›  Lucian

如何设计一个在数据下载完成后可以从服务器传递数据的类

  •  0
  • Lucian  · 技术社区  · 7 年前

    我是Android开发新手,最近我在学习如何正确使用MVP模式。

    现在我面临一个棘手的问题,希望能从这里得到一些有用的建议或解决方案。

    首先,这是我的演示者

    public class MVPPresenter {
    
        private MVPView mvpView;
    
        public MVPPresenter(MVPView mvpView) {
            this.mvpView = mvpView;
        }
    
        public void loadData() {
            mvpView.startLoading();
            final List<MVPModel> list = new ArrayList<>();
            //the part that I trying to extract starts here.
            Call call = DataRetriever.getDataByGet(URLCombiner.GET_FRONT_PAGE_ITEMS);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    mvpView.errorLoading();
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        try {
                            JSONObject result = new JSONObject(response.body().string());
                            int errorCode = result.getInt("ErrorCode");
                            if (errorCode == 0) {
                                JSONArray value = result.getJSONObject("Value").getJSONArray("hot");
                                for (int i = 0; i < value.length(); i++) {
                                    MVPModel mvpModel = new MVPModel();
                                    String name = null;
                                    String image = null;
                                    try {
                                        name = value.getJSONObject(i).getString("title");
                                        image = URLCombiner.IP + value.getJSONObject(i).getString("pic");
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    mvpModel.setName(name);
                                    mvpModel.setImage(image);
                                    list.add(mvpModel);
                                }
                                if (list.size() > 0) {
                                    mvpView.successLoading(list);
                                    mvpView.finishLoading();
                                } else {
                                    mvpView.errorLoading();
                                }
                            } else {
                                mvpView.errorLoading();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        mvpView.errorLoading();
                    }
                }
            });
            //the part that I trying to extract ends here.
        }
    }
    

    我想知道当数据下载完成后,如何将来自服务器的异步数据传递给演示者。

    public class LoadServerData {
    
        private static JSONArray arrayData = new JSONArray();
    
        public static JSONArray getServerData() {
            Call call = DataRetriever.getDataByGet(URLCombiner.GET_FRONT_PAGE_ITEMS);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
    
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        try {
                            JSONObject result = new JSONObject(response.body().string());
                            int errorCode = result.getInt("ErrorCode");
                            if (errorCode == 0) {
                                arrayData = result.getJSONObject("Value").getJSONArray("hot");  //the data I would like to return.
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            return arrayData;   //this is gonna be an empty data.
        }
    }
    

    我读了一些文章,可能可以解决我的问题,但仍然没有得到任何好的答案。也许我用错了关键字。希望你们能给我一些想法或解决方案来帮助我或激励我。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Alberto S.    7 年前

    创建一个简单的 Listener

    public class LoadServerData {
    
        public static interface Listener {
            public void onSuccess(JSONArray data);
            public void onError(Exception error);
        }
    
        public static void getServerData(Listener listener) {
            Call call = DataRetriever.getDataByGet(URLCombiner.GET_FRONT_PAGE_ITEMS);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    listener.onError(e);
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        try {
                            JSONObject result = new JSONObject(response.body().string());
                            int errorCode = result.getInt("ErrorCode");
                            if (errorCode == 0) {
                                JSONArray arrayData = result.getJSONObject("Value").getJSONArray("hot");  //the data I would like to return.
                                listener.onSuccess(arrayData);
                            } else {
                                listener.onError(...);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            listener.onError(e);
                        }
                    } else {
                        listener.onError(...);
                    }
                }
            });
        }
    }