MainActivity:
public class MainActivity extends Activity implements View.OnClickListener { .... @Override
public void onClick(View v) {
MultiAsynctaskNetwork network = new MultiAsynctaskNetwork(networkInterface);
network.execute();
} private NetworkInterface networkInterface = new NetworkInterface() {
@Override
public void onResult(String result) {
mTvResult.setText(result);
}
}; }
NetworkInterface:
public interface NetworkInterface { void onResult(String result); }
MultiAsynctaskNetwork:
public class MultiAsynctaskNetwork extends MultiAsynctask<Void, Integer, String> { private NetworkInterface mInterface; public MultiAsynctaskNetwork(NetworkInterface networkInterface) {
this.mInterface = networkInterface;
} @Override
protected String onExecuteTask(Void... params) {
HttpURLConnection connection = null;
try {
URL url = new URL("http://blog.csdn.net/yanzhenjie1003");
connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();
if (responseCode == ) {
int len = ;
byte[] buffer = new byte[];
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
InputStream inputStream = new BufferedInputStream(connection.getInputStream());
while ((len = inputStream.read(buffer)) != -) {
arrayOutputStream.write(buffer, , len);
}
inputStream.close();
arrayOutputStream.flush();
inputStream.close();
return new String(arrayOutputStream.toByteArray());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null)
connection.disconnect();
}
return "请求网络失败";
} @Override
public void onResult(String result) {// 拿到执行结果,直接更新UI
mInterface.onResult(result);
} }
核心类MultiAsynctask:
public abstract class MultiAsynctask<Param, Update, Result> { /**
* 更新的what
*/
private static final int WHAT_UPDATE = 0x01; /**
* 发送结果的what
*/
private static final int WHAT_RESULT = 0x02; /**
* 默认的线程池
*/
private static ExecutorService sExecutorService; /**
* 默认并发大小
*/
private static final int DEFAULT_POOL_SIZE = ; /**
* 发送结果的Handler
*/
private static Handler sHandler; /**
* Handler的锁
*/
private static Object HANDLER_LOCK = new Object(); /**
* 本地异步任务的执行器
*/
private ExecutorService mExecutorService = null; public MultiAsynctask() {
this(getDufaultExecutor());
} public MultiAsynctask(ExecutorService executorService) {
mExecutorService = executorService;
} /**
* 拿到默认的线程池
*
* @return
*/
private static ExecutorService getDufaultExecutor() {
synchronized (MultiAsynctask.class) {
if (sExecutorService == null)
sExecutorService = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE);
return sExecutorService;
}
} /**
* 设置默认的线程池
*
* @param executorService
*/
public static void setDefaultExecutor(ExecutorService executorService) {
synchronized (MultiAsynctask.class) {
sExecutorService = executorService;
}
} public static Handler getDefaultPoster() {
synchronized (HANDLER_LOCK) {
if (sHandler == null)
sHandler = new Poster();
return sHandler;
}
} /**
* 开始执行任务
*
* @param params
*/
public final void execute(Param... params) {
mExecutorService.execute(new Tasker(params));
} protected abstract Result onExecuteTask(Param... params); /**
* 发送进度更新到主线程
*
* @param update
*/
public final void onPostUpdate(Update update) {
Message.obtain();
Message message = getDefaultPoster().obtainMessage();
message.what = WHAT_UPDATE;
message.obj = new Messager<Param, Update, Result>(this, update, null);
message.sendToTarget();
} /**
* 当返回进度更新的时候
*
* @param update
*/
protected void onUpdate(Update update) {
} /**
* 发送进度执行结果到主线程
*
* @param result
*/
public final void onPostResult(Result result) {
Message.obtain();
Message message = getDefaultPoster().obtainMessage();
message.what = WHAT_RESULT;
message.obj = new Messager<Param, Update, Result>(this, null, result);
message.sendToTarget();
} /**
* 当返回执行结果的时候
*
* @param result
*/
protected void onResult(Result result) { } private static class Messager<Param, Update, Result> { private final MultiAsynctask<Param, Update, Result> asynctask; private final Update update; private final Result result; public Messager(MultiAsynctask<Param, Update, Result> asynctask, Update update, Result result) {
this.asynctask = asynctask;
this.update = update;
this.result = result;
} /**
* 调用当前MultiAsynctask的主线程更新方法
*/
public void onUpdate() {
asynctask.onUpdate(update);
} /**
* 调用当前MultiAsynctask的主线程结果方法
*/
public void onResult() {
asynctask.onResult(result);
} } /**
* <p>
* 线程间通信使者
* </p>
* Created in Mar 27, 2016 10:00:03 PM.
*
* @author Yolanda;
*/
private static class Poster extends Handler { public Poster() {
super(Looper.getMainLooper());
} @Override
public void handleMessage(Message msg) {
Messager<?, ?, ?> messageer = (Messager<?, ?, ?>) msg.obj;
if (msg.what == WHAT_RESULT) {
messageer.onResult();
} else if (msg.what == WHAT_UPDATE) {
messageer.onUpdate();
}
}
} /**
* <p>
* 任务执行器
* </p>
* Created in Mar 27, 2016 10:03:44 PM.
*
* @author Yolanda;
*/
private class Tasker implements Runnable { private Param[] params; public Tasker(Param... params) {
this.params = params;
} @Override
public void run() {
Result result = onExecuteTask(params);
onPostResult(result);
}
} }