android数据回传多个页面_请教一个界面同时获取多个接口数据的问题

在Android中,为了获取多个接口数据,开发者通常会面临选择同步或异步方式的问题。文中讨论了两种同步实现:直接使用子线程和线程池。通过创建子线程或利用线程池来并发请求接口,可以提高性能。同时,文章提出了对更优解决方案的寻求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我现在有一个界面需要同时访问两个接口从服务器获取数据,是用同步还是异步的方式好.我用了两种方法,都是同步的.第一种是创建一个子线程,同时把两个接口放在里面做网络访问;第二种是创建线程池去访问.请问同步方式还是异步方式访问好?有更优的方法吗?

第一种方法的代码:

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 workQueue = null;//阻塞队列

// workQueue = new ArrayBlockingQueue(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);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值