因为微博程序中客户端与服务器端的数据传递有时会耗时很长,所以采用多进程异步处理,就是界面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 { } }
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); } } // 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(); } } } } }</string></string></string>
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.

使用Java的String.valueOf()函数将基本数据类型转换为字符串在Java开发中,当我们需要将基本数据类型转换为字符串时,一种常见的方法是使用String类的valueOf()函数。这个函数可以接受基本数据类型的参数,并返回对应的字符串表示。在本文中,我们将探讨如何使用String.valueOf()函数进行基本数据类型转换,并提供一些代码示例来

char数组转string的方法:可以通过赋值来实现,使用{char a[]=" abc d\0efg ";string s=a;}语法,让char数组对string直接赋值,执行代码即可完成转换。

每年Apple发布新的iOS和macOS大版本之前,用户都可以提前几个月下载测试版抢先体验一番。由于公众和开发人员都使用该软件,所以苹果公司为两者推出了developer和public版即开发者测试版的公共测试版。iOS的developer版和public版有什么区别呢?从字面上的意思来说,developer版是开发者测试版,public版是公共测试版。developer版和public版面向的对象不同。developer版是苹果公司给开发者测试使用的,需要苹果开发者帐号才可以收到下载并升级,是

使用Java的String.replace()函数替换字符串中的字符(串)在Java中,字符串是不可变的对象,这意味着一旦创建了一个字符串对象,就无法修改它的值。但是,你可能会遇到需要替换字符串中的某些字符或者字符串的情况。这时候,我们可以使用Java的String类中的replace()方法来实现字符串的替换。String类的replace()方法有两种重

大家好,今天给大家分享java基础知识之String。String类的重要性就不必说了,可以说是我们后端开发用的最多的类,所以,很有必要好好来聊聊它。

使用Java的String.length()函数获取字符串的长度在Java编程中,字符串是一种非常常见的数据类型,我们经常需要获取字符串的长度,即字符串中字符的个数。在Java中,我们可以使用String类的length()函数来获取字符串的长度。下面是一个简单的示例代码:publicclassStringLengthExample{publ

一、认识String1.JDK中的String首先我们看看JDK中的String类源码,它实现了很多接口,可以看到String类被final修饰了,这就说明String类不可以被继承,String不存在子类,这样所有使用JDK的人,用到的String类都是同一个,如果String允许被继承,每个人都可以对String进行扩展,每个人使用的String都不是同一个版本,两个不同的人使用相同的方法,表现出不同的结果,这就导致代码没办法进行开发了继承和方法覆写在带来灵活性的同时,也会带来很多子类行为不

String中split方法使用String的split()方法用于按传入的字符或字符串对String进行拆分,返回拆分之后的数组。1、一般用法用一般的字符,例如@或,等符号做分隔符时:Stringaddress="上海@上海市@闵行区@吴中路";String[]splitAddr=address.split("@");System.out.println(splitAddr[0]+splitAddr[1]+splitAddr[2]+splitAddr[3


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools