Home > Backend Development > PHP Tutorial > Weibo Development 2 Client BaseTask and BaseTaskPool

Weibo Development 2 Client BaseTask and BaseTaskPool

WBOY
Release: 2016-08-08 09:31:04
Original
1035 people have browsed it
  因为微博程序中客户端与服务器端的数据传递有时会耗时很长,所以采用多进程异步处理,就是界面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 {

	}
	
}
Copy after login

The following is BaseTaskPool, the relevant code of the task pool.

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<String, String> taskArgs, BaseTask baseTask, int delayTime) {
		baseTask.setId(taskId);
		try {
			<span>taskPool.execute(new TaskThread(context, taskUrl, taskArgs, baseTask, delayTime));</span>
		} 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 {
			<span>taskPool.execute(new TaskThread(context, taskUrl, null, baseTask, delayTime));</span>
		} catch (Exception e) {
			taskPool.shutdown();
		}
	}
	
	// custom task
	public void addTask (int taskId, BaseTask baseTask, int delayTime) {
		baseTask.setId(taskId);
		try {
			<span>taskPool.execute(new TaskThread(context, null, null, baseTask, delayTime));</span>
		} catch (Exception e) {
			taskPool.shutdown();
		}
	}
	异步创建任务进程
	// task thread logic
	private class TaskThread implements Runnable {
		private Context context;
		private String taskUrl;
		private HashMap<String, String> taskArgs;
		private BaseTask baseTask;
		private int delayTime = 0;
		public TaskThread(Context context, String taskUrl, HashMap<String, String> 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);
						}</span>
					}
					// remote task
					if (httpResult != null) {
						baseTask.onComplete(httpResult);
					// local task
					} else {
						baseTask.onComplete();
					}
				} catch (Exception e) {
					baseTask.onError(e.getMessage());
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					baseTask.onStop();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
Copy after login


The above introduces the BaseTask and BaseTaskPool of Weibo Development 2 client, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template