鍍金池/ 問(wèn)答/Android/ 請(qǐng)教一個(gè)界面同時(shí)獲取多個(gè)接口數(shù)據(jù)的問(wèn)題

請(qǐng)教一個(gè)界面同時(shí)獲取多個(gè)接口數(shù)據(jù)的問(wèn)題

我現(xiàn)在有一個(gè)界面需要同時(shí)訪問(wèn)兩個(gè)接口從服務(wù)器獲取數(shù)據(jù),是用同步還是異步的方式好.我用了兩種方法,都是同步的.第一種是創(chuàng)建一個(gè)子線程,同時(shí)把兩個(gè)接口放在里面做網(wǎng)絡(luò)訪問(wèn);第二種是創(chuàng)建線程池去訪問(wèn).請(qǐng)問(wèn)同步方式還是異步方式訪問(wèn)好?有更優(yōu)的方法嗎?
第一種方法的代碼:

 new Thread(new Runnable() {
            @Override
            public void run() {
                String url = ConstantsUtils.HEAD_URL + "/video";
                try {
                    Response response = NetWorkRequestUtils.postRequest(url, new HomeDataObject());//接口一
                    if (response.isSuccessful()) {
                        String json = response.body().string();
                        Log.d(TAG, "布局一成功json=" + json);
                        PreferenceUtils.putString(getActivity(), url, json);//緩存JSON數(shù)據(jù)
                        setInitData(json);//獲取到j(luò)son數(shù)據(jù)后回調(diào)
                    } else {
                        Log.d(TAG, "布局一請(qǐng)求失敗222222");
                        Message obtain = Message.obtain();
                        obtain.what = SHOW_INIT_DATA_FAIL;
                        mHandler.sendMessage(obtain);
                    }
                } catch (Exception e) {
                    Log.d(TAG, "布局一請(qǐng)求失敗33333333" + "e.printStackTrace()");
                    Message obtain = Message.obtain();
                    obtain.what = SHOW_INIT_DATA_FAIL;
                    mHandler.sendMessage(obtain);
                    e.printStackTrace();
                }

                //二:獲取多布局相關(guān)推薦數(shù)據(jù),當(dāng)前首頁(yè)模塊channelid不用傳
                try {
                    Response response = NetWorkRequestUtils.postRequest(ConstantsUtils.HEAD_URL + "/video/recoList", new RelatedRecommendedObject());//接口二
                    if (response.isSuccessful()) {
                        String json = response.body().string();
                        Log.d(TAG, "布局二成功=" + json);
                        setRelatedRecommendedData(json);//設(shè)置相關(guān)推薦數(shù)據(jù)
                    } else {
                        Log.d(TAG, "布局二失敗11111111111111");
                        Message obtain = Message.obtain();
                        obtain.what = SHOW_LOAD_MORE_FAIL;
                        mHandler.sendMessage(obtain);
                    }
                } catch (Exception e) {
                    Log.d(TAG, "布局二失敗22222222222");
                    Message obtain = Message.obtain();
                    obtain.what = SHOW_LOAD_MORE_FAIL;
                    mHandler.sendMessage(obtain);
                    e.printStackTrace();
                }
            }
        }).start();

第二種方法:

 private void getHomeInitData() {
        ThreadManager.getNormalPool().execute(new InitListTask());//獲取布局一初始列表的數(shù)據(jù)
        ThreadManager.getNormalPool().execute(new RelatedRecommendedTask());//獲取布局二相關(guān)推薦的數(shù)據(jù)
    }

    /** 獲取布局一初始列表的數(shù)據(jù)    */
    private class InitListTask implements Runnable {
        @Override
        public void run() {
            String url = ConstantsUtils.HEAD_URL + "/video";
            try {
                Response response = NetWorkRequestUtils.postRequest(url, new HomeDataObject());
                if (response.isSuccessful()) {
                    String json = response.body().string();
                    Log.d(TAG, "布局一成功json=" + json);
                    PreferenceUtils.putString(getActivity(), url, json);//緩存JSON數(shù)據(jù)
                    setInitData(json);//獲取到j(luò)son數(shù)據(jù)后回調(diào)
                } else {
                    Log.d(TAG, "布局一請(qǐng)求失敗222222");
                    Message obtain = Message.obtain();
                    obtain.what = SHOW_INIT_DATA_FAIL;
                    mHandler.sendMessage(obtain);
                }
            } catch (Exception e) {
                Log.d(TAG, "布局一請(qǐng)求失敗33333333" + "e.printStackTrace()");
                Message obtain = Message.obtain();
                obtain.what = SHOW_INIT_DATA_FAIL;
                mHandler.sendMessage(obtain);
                e.printStackTrace();
            }
        }
    }

    private class RelatedRecommendedTask implements Runnable{
        @Override
        public void run() {
            try {
                Response response = NetWorkRequestUtils.postRequest(ConstantsUtils.HEAD_URL + "/video/recoList", new RelatedRecommendedObject());
                if (response.isSuccessful()) {
                    String json = response.body().string();
                    Log.d(TAG, "布局二成功=" + json);
                    setRelatedRecommendedData(json);//設(shè)置相關(guān)推薦數(shù)據(jù)
                } else {
                    Log.d(TAG, "布局二失敗11111111111111");
                    Message obtain = Message.obtain();
                    obtain.what = SHOW_LOAD_MORE_FAIL;
                    mHandler.sendMessage(obtain);
                }
            } catch (Exception e) {
                Log.d(TAG, "布局二失敗22222222222");
                Message obtain = Message.obtain();
                obtain.what = SHOW_LOAD_MORE_FAIL;
                mHandler.sendMessage(obtain);
                e.printStackTrace();
            }
        }
    }

ThreadManager類(lèi)是這樣寫(xiě)的:

public class ThreadManager {

    private static ThreadPoolProxy mNormalPool   = new ThreadPoolProxy(3, 3, 5 * 1000);
    private static ThreadPoolProxy mDownloadPool = new ThreadPoolProxy(3, 3, 5 * 1000);
    public static ThreadPoolProxy getNormalPool() {
        return mNormalPool;
    }
    public static ThreadPoolProxy getDownloadPool() {
        return mDownloadPool;
    }

    public static class ThreadPoolProxy {
        private final int                mCorePoolSize;
        private final int                mMaximumPoolSize;
        private final long               mKeepAliveTime;
        private       ThreadPoolExecutor mPool;
        public ThreadPoolProxy(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
            this.mCorePoolSize = corePoolSize;
            this.mMaximumPoolSize = maximumPoolSize;
            this.mKeepAliveTime = keepAliveTime;
        }
        private void initPool() {
            if (mPool == null || mPool.isShutdown()) {
                //                int corePoolSize = 1;//核心線程池大小
                //                int maximumPoolSize = 3;//最大線程池大小
                //                long keepAliveTime = 5 * 1000;//保持存活的時(shí)間
                TimeUnit                unit      = TimeUnit.MILLISECONDS;//單位
                BlockingQueue<Runnable> workQueue = null;//阻塞隊(duì)列

                //                workQueue = new ArrayBlockingQueue<Runnable>(3);//FIFO,大小有限制
                workQueue = new LinkedBlockingQueue();//
                //                workQueue = new PriorityBlockingQueue();

                ThreadFactory threadFactory = Executors.defaultThreadFactory();//線程工廠

                RejectedExecutionHandler handler = null;//異常捕獲器

                //                handler = new ThreadPoolExecutor.DiscardOldestPolicy();//去掉隊(duì)列中首個(gè)任務(wù),將新加入的放到隊(duì)列中去
                //                handler = new ThreadPoolExecutor.AbortPolicy();//觸發(fā)異常
                handler = new ThreadPoolExecutor.DiscardPolicy();//不做任何處理
                //                handler = new ThreadPoolExecutor.CallerRunsPolicy();//直接執(zhí)行,不歸線程池控制,在調(diào)用線程中執(zhí)行
                //                new Thread(task).start();

                mPool = new ThreadPoolExecutor(mCorePoolSize,
                                               mMaximumPoolSize,
                                               mKeepAliveTime,
                                               unit,
                                               workQueue,
                                               threadFactory,
                                               handler);
            }
        }

        /**
         * 執(zhí)行任務(wù)
         * @param task
         */
        public void execute(Runnable task) {
            initPool();
            //執(zhí)行任務(wù)
            mPool.execute(task);
        }
        public Future<?> submit(Runnable task) {
            initPool();
            return mPool.submit(task);
        }
        public void remove(Runnable task) {
            if (mPool != null && !mPool.isShutdown()) {
                mPool.getQueue()
                     .remove(task);
            }
        }
    }
}
回答
編輯回答
憶往昔
Rx 了解一下?

從代碼可以看出:推薦數(shù)據(jù)拉取是后于主體數(shù)據(jù)拉取的,如果主體數(shù)據(jù)還沒(méi)顯示就已經(jīng)顯示推薦數(shù)據(jù)了,那這個(gè)交互就很奇怪了,除非主體數(shù)據(jù)為空。所以,用簡(jiǎn)單異步請(qǐng)求是不合適的。這時(shí)候,Rx就可以發(fā)揮它的神奇了,如下:

RxJava + RxAndroid + Retrofit + RxLifecycle 可以很好地解決這類(lèi)需求。

p.s. 以上在github都可以找到

2018年1月16日 04:31
編輯回答
蟲(chóng)児飛

這個(gè)不僅僅是一個(gè)頁(yè)面2個(gè)接口的問(wèn)題,如果項(xiàng)目里有30+接口,或者更多,你這么寫(xiě)累不累,維護(hù)起來(lái)頭疼不?建議封裝到NetWorkRequestUtils,發(fā)起請(qǐng)求傳遞一個(gè)callback接受結(jié)果,NetWorkRequestUtils內(nèi)部用線程池。
至于你說(shuō)的兩個(gè)接口是兩個(gè)線程處理,還是一個(gè)線程串行,這個(gè)看你業(yè)務(wù)上兩個(gè)接口有沒(méi)有順序依賴,如果沒(méi)有,建議分開(kāi)處理。

2018年4月8日 19:17