欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

微博开发2客户端的BaseTask以及BaseTaskPool

程序员文章站 2022-05-03 13:15:34
...
  因为微博程序中客户端与服务器端的数据传递有时会耗时很长,所以采用多进程异步处理,就是界面UI与数据的发送接收不在一个进程里。每次数据传输的时候会开一个新的线程。
    BaseTask就是这个记录异步任务属性的类
package com.app.demos.base;

public class BaseTask {

	public static final int TASK_COMPLETE = 0;   //任务完成
	public static final int NETWORK_ERROR = 1;   //网络错误
	public static final int SHOW_LOADBAR = 2;    //显示下载条
	public static final int HIDE_LOADBAR = 3;    //不显示下载条
	public static final int SHOW_TOAST = 4;      //显示Toast
	public static final int LOAD_IMAGE = 5;      //加载图片
	
	private int id = 0;
	private String name = "";
	
	public BaseTask() {}
	
	public int getId () {
		return this.id;
	}
	
	public void setId (int id) {
		this.id = id;
	}
	
	public String getName () {
		return this.name;
	}
	
	public void setName (String name) {
		this.name = name;
	}
	
	public void onStart () {

	}
	
	public void onComplete () {

	}
	
	public void onComplete (String httpResult) {

	}
	
	public void onError (String error) {

	}
	
	public void onStop () throws Exception {

	}
	
}

下面就是BaseTaskPool,任务池的相关代码。

package com.app.demos.base;

import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.Context;
import com.app.demos.util.HttpUtil;

import com.app.demos.util.AppClient;

public class BaseTaskPool {
	
	// task thread pool
	static private ExecutorService taskPool;
	
	// for HttpUtil.getNetType
	private Context context;
	
	public BaseTaskPool (BaseUi ui) {                       //初始建立缓冲池
		this.context = ui.getContext();
		taskPool = Executors.newCachedThreadPool();
	}
	根据不同的需求重载了addTask方法三次
	// http post task with params
	public void addTask (int taskId, String taskUrl, HashMap taskArgs, BaseTask baseTask, int delayTime) {
		baseTask.setId(taskId);
		try {
			taskPool.execute(new TaskThread(context, taskUrl, taskArgs, baseTask, delayTime));
		} catch (Exception e) {
			taskPool.shutdown();
		}
	}
	
	// http post task without params
	public void addTask (int taskId, String taskUrl, BaseTask baseTask, int delayTime) {
		baseTask.setId(taskId);
		try {
			taskPool.execute(new TaskThread(context, taskUrl, null, baseTask, delayTime));
		} catch (Exception e) {
			taskPool.shutdown();
		}
	}
	
	// custom task
	public void addTask (int taskId, BaseTask baseTask, int delayTime) {
		baseTask.setId(taskId);
		try {
			taskPool.execute(new TaskThread(context, null, null, baseTask, delayTime));
		} catch (Exception e) {
			taskPool.shutdown();
		}
	}
	异步创建任务进程
	// task thread logic
	private class TaskThread implements Runnable {
		private Context context;
		private String taskUrl;
		private HashMap taskArgs;
		private BaseTask baseTask;
		private int delayTime = 0;
		public TaskThread(Context context, String taskUrl, HashMap taskArgs, BaseTask baseTask, int delayTime) {
			this.context = context;
			this.taskUrl = taskUrl;
			this.taskArgs = taskArgs;
			this.baseTask = baseTask;
			this.delayTime = delayTime;
		}
		@Override
		public void run() {
			try {
				baseTask.onStart();
				String httpResult = null;
				// set delay time
				if (this.delayTime > 0) {
					Thread.sleep(this.delayTime);
				}
				try {
					// remote task
					if (this.taskUrl != null) {
						// init app client
						AppClient client = new AppClient(this.taskUrl); // 用到了上一讲的AppClient封装了基本的http 的post与get方法
						if (HttpUtil.WAP_INT == HttpUtil.getNetType(context)) {
							client.useWap();
						}
						// http get
						if (taskArgs == null) {
							httpResult = client.get();
						// http post
						} else {
							httpResult = client.post(this.taskArgs);
						}