我现在有一个界面需要同时访问两个接口从服务器获取数据,是用同步还是异步的方式好.我用了两种方法,都是同步的.第一种是创建一个子线程,同时把两个接口放在里面做网络访问;第二种是创建线程池去访问.请问同步方式还是异步方式访问好?有更优的方法吗?
第一种方法的代码:
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数据
setInitData(json);//获取到json数据后回调
} else {
Log.d(TAG, "布局一请求失败222222");
Message obtain = Message.obtain();
obtain.what = SHOW_INIT_DATA_FAIL;
mHandler.sendMessage(obtain);
}
} catch (Exception e) {
Log.d(TAG, "布局一请求失败33333333" + "e.printStackTrace()");
Message obtain = Message.obtain();
obtain.what = SHOW_INIT_DATA_FAIL;
mHandler.sendMessage(obtain);
e.printStackTrace();
}
//二:获取多布局相关推荐数据,当前首页模块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);//设置相关推荐数据
} 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());//获取布局一初始列表的数据
ThreadManager.getNormalPool().execute(new RelatedRecommendedTask());//获取布局二相关推荐的数据
}
/** 获取布局一初始列表的数据 */
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数据
setInitData(json);//获取到json数据后回调
} else {
Log.d(TAG, "布局一请求失败222222");
Message obtain = Message.obtain();
obtain.what = SHOW_INIT_DATA_FAIL;
mHandler.sendMessage(obtain);
}
} catch (Exception e) {
Log.d(TAG, "布局一请求失败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);//设置相关推荐数据
} 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类是这样写的:
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;//保持存活的时间
TimeUnit unit = TimeUnit.MILLISECONDS;//单位
BlockingQueue<Runnable> workQueue = null;//阻塞队列
// workQueue = new ArrayBlockingQueue<Runnable>(3);//FIFO,大小有限制
workQueue = new LinkedBlockingQueue();//
// workQueue = new PriorityBlockingQueue();
ThreadFactory threadFactory = Executors.defaultThreadFactory();//线程工厂
RejectedExecutionHandler handler = null;//异常捕获器
// handler = new ThreadPoolExecutor.DiscardOldestPolicy();//去掉队列中首个任务,将新加入的放到队列中去
// handler = new ThreadPoolExecutor.AbortPolicy();//触发异常
handler = new ThreadPoolExecutor.DiscardPolicy();//不做任何处理
// handler = new ThreadPoolExecutor.CallerRunsPolicy();//直接执行,不归线程池控制,在调用线程中执行
// new Thread(task).start();
mPool = new ThreadPoolExecutor(mCorePoolSize,
mMaximumPoolSize,
mKeepAliveTime,
unit,
workQueue,
threadFactory,
handler);
}
}
/**
* 执行任务
* @param task
*/
public void execute(Runnable task) {
initPool();
//执行任务
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就可以发挥它的神奇了,如下:
RxJava
+RxAndroid
+Retrofit
+RxLifecycle
可以很好地解决这类需求。p.s. 以上在github都可以找到