|
|
|
|
@ -16,190 +16,248 @@
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
|
import android.accounts.AccountManager;
|
|
|
|
|
import android.accounts.AccountManagerFuture;
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.gtask.data.Node;
|
|
|
|
|
import net.micode.notes.gtask.data.Task;
|
|
|
|
|
import net.micode.notes.gtask.data.TaskList;
|
|
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException;
|
|
|
|
|
import net.micode.notes.gtask.exception.NetworkFailureException;
|
|
|
|
|
import net.micode.notes.tool.GTaskStringUtils;
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity;
|
|
|
|
|
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
import org.apache.http.cookie.Cookie;
|
|
|
|
|
import org.apache.http.impl.client.BasicCookieStore;
|
|
|
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
|
|
import org.apache.http.params.BasicHttpParams;
|
|
|
|
|
import org.apache.http.params.HttpConnectionParams;
|
|
|
|
|
import org.apache.http.params.HttpParams;
|
|
|
|
|
import org.apache.http.params.HttpProtocolParams;
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
|
import java.util.zip.Inflater;
|
|
|
|
|
import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.accounts.Account; // 谷歌账号相关:存储账号名称、类型等信息
|
|
|
|
|
import android.accounts.AccountManager; // 账号管理器:获取手机里的谷歌账号、登录凭证等
|
|
|
|
|
import android.accounts.AccountManagerFuture; // 账号操作结果:异步获取账号登录凭证的返回值
|
|
|
|
|
import android.app.Activity; // 页面相关:用来关联登录的页面
|
|
|
|
|
import android.os.Bundle; // 数据容器:用来存放账号登录凭证等数据
|
|
|
|
|
import android.text.TextUtils; // 文本工具:判断字符串是否为空、去除空格等
|
|
|
|
|
import android.util.Log; // 日志工具:打印调试信息,方便查找问题
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.gtask.data.Node; // 基础数据节点:任务/任务列表的通用数据模型
|
|
|
|
|
import net.micode.notes.gtask.data.Task; // 任务数据模型:存储单个GTask任务的信息
|
|
|
|
|
import net.micode.notes.gtask.data.TaskList; // 任务列表数据模型:存储GTask任务列表的信息
|
|
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException; // 操作失败异常:创建/删除任务等操作失败时抛出
|
|
|
|
|
import net.micode.notes.gtask.exception.NetworkFailureException; // 网络失败异常:网络不通或请求失败时抛出
|
|
|
|
|
import net.micode.notes.tool.GTaskStringUtils; // 字符串工具:存储GTask接口用到的固定字符串(比如接口参数名)
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity; // 便签设置页面:获取用户设置的同步账号名称
|
|
|
|
|
|
|
|
|
|
import org.apache.http.HttpEntity; // 网络请求实体:存储网络请求/响应的内容
|
|
|
|
|
import org.apache.http.HttpResponse; // 网络响应:存储服务器返回的所有数据(状态码、内容等)
|
|
|
|
|
import org.apache.http.client.ClientProtocolException; // 客户端协议异常:网络请求格式错误时抛出
|
|
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity; // 表单实体:把请求参数封装成表单格式
|
|
|
|
|
import org.apache.http.client.methods.HttpGet; // GET请求:向服务器获取数据的请求方式
|
|
|
|
|
import org.apache.http.client.methods.HttpPost; // POST请求:向服务器提交数据的请求方式
|
|
|
|
|
import org.apache.http.cookie.Cookie; // 登录凭证:用来保持登录状态,不用每次都输账号密码
|
|
|
|
|
import org.apache.http.impl.client.BasicCookieStore; // Cookie容器:存储登录后的Cookie信息
|
|
|
|
|
import org.apache.http.impl.client.DefaultHttpClient; // 网络请求客户端:发送GET/POST请求的工具
|
|
|
|
|
import org.apache.http.message.BasicNameValuePair; // 键值对:存储请求参数(比如"key=value")
|
|
|
|
|
import org.apache.http.params.BasicHttpParams; // 网络参数容器:存储网络请求的配置(比如超时时间)
|
|
|
|
|
import org.apache.http.params.HttpConnectionParams; // 连接参数:设置网络连接的超时时间
|
|
|
|
|
import org.apache.http.params.HttpParams; // 网络参数:通用的网络配置参数
|
|
|
|
|
import org.apache.http.params.HttpProtocolParams; // 协议参数:设置网络请求的协议配置
|
|
|
|
|
import org.json.JSONArray; // JSON数组:存储一组格式化数据(比如多个任务信息)
|
|
|
|
|
import org.json.JSONException; // JSON解析异常:数据格式错误导致解析失败时抛出
|
|
|
|
|
import org.json.JSONObject; // JSON对象:存储键值对格式的数据(比如单个任务信息)
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader; // 缓冲读取器:高效读取服务器返回的文本内容
|
|
|
|
|
import java.io.IOException; // IO异常:读取/写入数据失败时抛出
|
|
|
|
|
import java.io.InputStream; // 输入流:读取服务器返回的原始数据
|
|
|
|
|
import java.io.InputStreamReader; // 输入流读取器:把原始字节数据转换成文本数据
|
|
|
|
|
import java.util.LinkedList; // 链表:存储请求参数的容器,有序且方便添加
|
|
|
|
|
import java.util.List; // 集合:存储一组数据的通用接口
|
|
|
|
|
import java.util.zip.GZIPInputStream; // GZIP解压流:解压服务器返回的GZIP格式数据
|
|
|
|
|
import java.util.zip.Inflater; // 解压工具:处理DEFLATE格式的解压
|
|
|
|
|
import java.util.zip.InflaterInputStream; // DEFLATE解压流:解压服务器返回的DEFLATE格式数据
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTask客户端工具类
|
|
|
|
|
* 通俗说:这个类专门负责和谷歌任务(GTask)服务器打交道,比如登录、创建任务、获取任务列表等
|
|
|
|
|
* 特点:整个APP只有这一个实例(不会创建多个重复对象),节省内存
|
|
|
|
|
*/
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
// 日志标签:打印日志时用来标识是这个类的日志,方便查找问题
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
// GTask基础地址:谷歌任务的根地址
|
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
|
|
|
|
|
|
// GTask GET请求地址:用来从服务器获取数据(比如任务列表)
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
|
|
|
|
|
|
// GTask POST请求地址:用来向服务器提交数据(比如创建任务、删除任务)
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
|
|
|
|
|
|
// 本类的唯一实例:保证整个APP只有一个GTaskClient对象
|
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
|
|
// 网络请求客户端:用来发送GET/POST请求,和服务器通信
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
|
|
|
|
|
// 实际使用的GET请求地址:可能是默认地址,也可能是自定义域名地址
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
|
|
|
|
|
// 实际使用的POST请求地址:可能是默认地址,也可能是自定义域名地址
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
|
|
// 客户端版本号:和服务器交互时需要传递的版本信息,用来兼容不同版本
|
|
|
|
|
private long mClientVersion;
|
|
|
|
|
|
|
|
|
|
// 登录状态标记:true=已登录,false=未登录
|
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
|
|
|
|
|
// 上次登录时间:用来判断登录状态是否过期
|
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
|
|
|
|
|
// 操作ID:每次和服务器交互的操作都会分配一个唯一ID,自增累加
|
|
|
|
|
private int mActionId;
|
|
|
|
|
|
|
|
|
|
// 同步账号:当前用来同步GTask的谷歌账号
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
// 更新数据数组:存储待提交的更新操作(比如修改任务、新增任务),批量提交更高效
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 私有构造方法
|
|
|
|
|
* 通俗说:不让外部直接创建这个类的对象,只能通过getInstance()获取唯一实例
|
|
|
|
|
*/
|
|
|
|
|
private GTaskClient() {
|
|
|
|
|
mHttpClient = null;
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
|
mClientVersion = -1;
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
mLastLoginTime = 0;
|
|
|
|
|
mActionId = 1;
|
|
|
|
|
mAccount = null;
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
mHttpClient = null; // 初始化网络请求客户端为null
|
|
|
|
|
mGetUrl = GTASK_GET_URL; // 默认使用GTask默认GET地址
|
|
|
|
|
mPostUrl = GTASK_POST_URL; // 默认使用GTask默认POST地址
|
|
|
|
|
mClientVersion = -1; // 客户端版本号初始化为-1(未获取)
|
|
|
|
|
mLoggedin = false; // 初始状态为未登录
|
|
|
|
|
mLastLoginTime = 0; // 上次登录时间初始化为0
|
|
|
|
|
mActionId = 1; // 操作ID从1开始自增
|
|
|
|
|
mAccount = null; // 同步账号初始化为null
|
|
|
|
|
mUpdateArray = null; // 更新数据数组初始化为null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取GTaskClient的唯一实例
|
|
|
|
|
* 通俗说:整个APP只能通过这个方法拿到GTaskClient对象,保证只有一个实例
|
|
|
|
|
* @return GTaskClient唯一实例
|
|
|
|
|
*/
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
|
// 如果实例为null,就创建一个新的(懒加载:用到时才创建)
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
|
}
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录GTask服务器
|
|
|
|
|
* 通俗说:验证谷歌账号,获取登录凭证,保持和服务器的登录状态
|
|
|
|
|
* @param activity 登录关联的页面(用来获取账号信息、处理登录回调)
|
|
|
|
|
* @return true=登录成功,false=登录失败
|
|
|
|
|
*/
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
|
// then we need to re-login
|
|
|
|
|
// 假设登录凭证5分钟过期,过期后需要重新登录
|
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
mLoggedin = false; // 登录过期,标记为未登录
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// need to re-login after account switch
|
|
|
|
|
// 如果已经登录,但账号和设置里的同步账号不一致(用户切换了账号),也需要重新登录
|
|
|
|
|
if (mLoggedin
|
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果已经登录且未过期、账号未切换,直接返回登录成功
|
|
|
|
|
if (mLoggedin) {
|
|
|
|
|
Log.d(TAG, "already logged in");
|
|
|
|
|
Log.d(TAG, "already logged in"); // 打印日志:已登录
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 记录本次登录时间
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis();
|
|
|
|
|
// 获取谷歌账号的登录凭证(令牌)
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false);
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
Log.e(TAG, "login google account failed"); // 打印日志:谷歌账号登录失败
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// login with custom domain if necessary
|
|
|
|
|
// 处理非gmail/googlemail域名的账号(自定义域名账号,需要切换请求地址)
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
|
.endsWith("googlemail.com"))) {
|
|
|
|
|
// 拼接自定义域名的请求地址
|
|
|
|
|
StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
|
|
|
|
|
int index = mAccount.name.indexOf('@') + 1;
|
|
|
|
|
int index = mAccount.name.indexOf('@') + 1; // 找到@符号的位置,截取域名后缀
|
|
|
|
|
String suffix = mAccount.name.substring(index);
|
|
|
|
|
url.append(suffix + "/");
|
|
|
|
|
mGetUrl = url.toString() + "ig";
|
|
|
|
|
mPostUrl = url.toString() + "r/ig";
|
|
|
|
|
mGetUrl = url.toString() + "ig"; // 自定义GET地址
|
|
|
|
|
mPostUrl = url.toString() + "r/ig"; // 自定义POST地址
|
|
|
|
|
|
|
|
|
|
// 使用自定义地址尝试登录GTask
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
mLoggedin = true; // 登录成功,标记为已登录
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to login with google official url
|
|
|
|
|
// 如果自定义地址登录失败,使用默认地址再次尝试登录
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
|
mGetUrl = GTASK_GET_URL; // 恢复默认GET地址
|
|
|
|
|
mPostUrl = GTASK_POST_URL; // 恢复默认POST地址
|
|
|
|
|
// 默认地址登录失败,返回false
|
|
|
|
|
if (!tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 所有登录逻辑完成,标记为已登录
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取谷歌账号的登录凭证(令牌)
|
|
|
|
|
* 通俗说:从手机的账号管理器中,获取指定谷歌账号的登录凭证,用来登录GTask
|
|
|
|
|
* @param activity 关联页面
|
|
|
|
|
* @param invalidateToken 是否失效旧凭证(旧凭证过期时需要设为true)
|
|
|
|
|
* @return 登录凭证(令牌),null=获取失败
|
|
|
|
|
*/
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
String authToken;
|
|
|
|
|
String authToken; // 登录凭证
|
|
|
|
|
// 获取手机的账号管理器
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
// 获取手机里所有的谷歌账号
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
|
|
|
|
|
|
// 如果没有谷歌账号,返回null
|
|
|
|
|
if (accounts.length == 0) {
|
|
|
|
|
Log.e(TAG, "there is no available google account");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取用户在便签设置里指定的同步账号名称
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
|
|
|
|
|
Account account = null;
|
|
|
|
|
// 遍历所有谷歌账号,找到和设置里一致的账号
|
|
|
|
|
for (Account a : accounts) {
|
|
|
|
|
if (a.name.equals(accountName)) {
|
|
|
|
|
account = a;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 找到匹配的账号,保存到成员变量
|
|
|
|
|
if (account != null) {
|
|
|
|
|
mAccount = account;
|
|
|
|
|
} else {
|
|
|
|
|
// 没找到匹配的账号,打印日志并返回null
|
|
|
|
|
Log.e(TAG, "unable to get an account with the same name in the settings");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the token now
|
|
|
|
|
// 获取账号的登录凭证
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
|
"goanna_mobile", null, activity, null, null);
|
|
|
|
|
try {
|
|
|
|
|
// 获取凭证返回结果
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
|
// 从结果中提取登录凭证
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
// 如果需要失效旧凭证,先失效再重新获取
|
|
|
|
|
if (invalidateToken) {
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 获取凭证失败,打印日志并设为null
|
|
|
|
|
Log.e(TAG, "get auth token failed");
|
|
|
|
|
authToken = null;
|
|
|
|
|
}
|
|
|
|
|
@ -207,16 +265,24 @@ public class GTaskClient {
|
|
|
|
|
return authToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 尝试登录GTask服务器
|
|
|
|
|
* 通俗说:先用当前凭证登录,失败的话就失效旧凭证,重新获取凭证再登录
|
|
|
|
|
* @param activity 关联页面
|
|
|
|
|
* @param authToken 登录凭证
|
|
|
|
|
* @return true=登录成功,false=登录失败
|
|
|
|
|
*/
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
|
// 先用当前凭证登录
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
// maybe the auth token is out of date, now let's invalidate the
|
|
|
|
|
// token and try again
|
|
|
|
|
// 登录失败,说明凭证过期,重新获取凭证
|
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 用新凭证再次尝试登录,还是失败就返回false
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
Log.e(TAG, "login gtask failed");
|
|
|
|
|
return false;
|
|
|
|
|
@ -225,54 +291,76 @@ public class GTaskClient {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 实际执行GTask登录逻辑
|
|
|
|
|
* 通俗说:用登录凭证发送请求,获取登录状态(Cookie)和客户端版本号
|
|
|
|
|
* @param authToken 登录凭证
|
|
|
|
|
* @return true=登录成功,false=登录失败
|
|
|
|
|
*/
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
// 设置网络连接超时时间:10秒(连接不上服务器就超时)
|
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
|
// 设置网络读取超时时间:15秒(服务器没返回数据就超时)
|
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
// 创建网络参数容器
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
|
// 设置连接超时时间
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
|
// 设置读取超时时间
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
|
// 初始化网络请求客户端,传入超时参数
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
|
// 创建Cookie容器,存储登录后的凭证
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
|
// 给网络客户端设置Cookie容器
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
|
// 关闭Expect-Continue协议,避免部分服务器不兼容
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
|
// 执行GTask登录请求
|
|
|
|
|
try {
|
|
|
|
|
// 拼接登录请求地址(带登录凭证)
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
// 创建GET请求
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
// 发送请求,获取服务器响应
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
|
// 获取登录后的Cookie,判断是否包含GTask的授权Cookie
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
if (cookie.getName().contains("GTL")) {
|
|
|
|
|
hasAuthCookie = true;
|
|
|
|
|
hasAuthCookie = true; // 包含授权Cookie
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!hasAuthCookie) {
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie"); // 打印警告:没有授权Cookie
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the client version
|
|
|
|
|
// 获取服务器返回的内容,解析出客户端版本号
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
// 找到JSON数据的起始和结束位置
|
|
|
|
|
int begin = resString.indexOf(jsBegin);
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
String jsString = null;
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
// 截取JSON数据字符串
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
}
|
|
|
|
|
// 解析JSON数据,获取客户端版本号
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
mClientVersion = js.getLong("v");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// JSON解析失败,打印日志并返回false
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// simply catch all exceptions
|
|
|
|
|
// 其他异常(比如网络异常),打印日志并返回false
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
@ -280,152 +368,230 @@ public class GTaskClient {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取自增的操作ID
|
|
|
|
|
* 通俗说:每次和服务器交互,都给操作分配一个唯一ID,用后自增(1→2→3→...)
|
|
|
|
|
* @return 唯一操作ID
|
|
|
|
|
*/
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
return mActionId++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建POST请求对象
|
|
|
|
|
* 通俗说:封装POST请求的公共配置(请求头),不用每次创建都重复写
|
|
|
|
|
* @return 配置好的POST请求对象
|
|
|
|
|
*/
|
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
|
// 设置请求内容类型:表单格式,编码为UTF-8
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
|
// 设置AT请求头:固定值1,GTask接口要求
|
|
|
|
|
httpPost.setHeader("AT", "1");
|
|
|
|
|
return httpPost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取网络响应的文本内容
|
|
|
|
|
* 通俗说:把服务器返回的原始数据(字节)转换成文本,还会处理压缩格式(GZIP/DEFLATE)
|
|
|
|
|
* @param entity 网络响应实体(存储服务器返回的内容)
|
|
|
|
|
* @return 服务器返回的文本内容
|
|
|
|
|
* @throws IOException 读取数据失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
// 获取响应内容的编码格式(判断是否是压缩格式)
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取响应的原始输入流
|
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
// 如果是GZIP压缩格式,解压后再读取
|
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
|
input = new GZIPInputStream(entity.getContent());
|
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
|
}
|
|
|
|
|
// 如果是DEFLATE压缩格式,解压后再读取
|
|
|
|
|
else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
|
Inflater inflater = new Inflater(true);
|
|
|
|
|
input = new InflaterInputStream(entity.getContent(), inflater);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 把字节输入流转换成文本输入流
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(input);
|
|
|
|
|
// 用缓冲读取器提高读取效率
|
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
StringBuilder sb = new StringBuilder(); // 存储读取到的文本内容
|
|
|
|
|
|
|
|
|
|
// 循环读取每一行文本,直到读取完毕
|
|
|
|
|
while (true) {
|
|
|
|
|
String buff = br.readLine();
|
|
|
|
|
if (buff == null) {
|
|
|
|
|
return sb.toString();
|
|
|
|
|
return sb.toString(); // 返回读取到的所有文本
|
|
|
|
|
}
|
|
|
|
|
sb = sb.append(buff);
|
|
|
|
|
sb = sb.append(buff); // 把每行文本添加到字符串中
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
input.close();
|
|
|
|
|
input.close(); // 关闭输入流,释放资源
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送POST请求到GTask服务器
|
|
|
|
|
* 通俗说:把封装好的JSON数据提交给服务器,获取返回结果,处理各种异常
|
|
|
|
|
* @param js 要提交的JSON数据(包含操作类型、参数等)
|
|
|
|
|
* @return 服务器返回的JSON结果
|
|
|
|
|
* @throws NetworkFailureException 网络失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
// 如果未登录,抛出异常
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建配置好的POST请求
|
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
|
try {
|
|
|
|
|
// 创建请求参数列表(存储"r=JSON字符串"这个参数)
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
|
// 把参数列表封装成表单实体
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
// 给POST请求设置请求实体
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
|
// 发送POST请求,获取服务器响应
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
// 获取响应的文本内容
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
// 把文本内容解析成JSON对象返回
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
// 客户端协议异常,打印日志并抛出网络失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// IO异常(比如网络断开),打印日志并抛出网络失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// JSON解析异常,打印日志并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("unable to convert response content to jsonobject");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 其他异常,打印日志并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("error occurs when posting request");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建单个GTask任务
|
|
|
|
|
* 通俗说:把本地任务数据提交到GTask服务器,创建新任务,并获取服务器返回的任务ID
|
|
|
|
|
* @param task 要创建的本地任务对象
|
|
|
|
|
* @throws NetworkFailureException 网络失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
// 先提交之前待处理的更新操作
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建POST请求的JSON数据
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 操作列表(存储创建任务的操作)
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 把创建任务的操作添加到操作列表
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
// 给JSON数据添加操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 给JSON数据添加客户端版本号
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
// 发送POST请求,获取服务器响应
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 从响应中获取创建任务的结果
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
// 把服务器返回的任务ID设置到本地任务对象中
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// JSON解析失败,打印日志并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建GTask任务列表
|
|
|
|
|
* 通俗说:把本地任务列表数据提交到GTask服务器,创建新任务列表,并获取服务器返回的列表ID
|
|
|
|
|
* @param tasklist 要创建的本地任务列表对象
|
|
|
|
|
* @throws NetworkFailureException 网络失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
// 先提交之前待处理的更新操作
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建POST请求的JSON数据
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 操作列表(存储创建任务列表的操作)
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 把创建任务列表的操作添加到操作列表
|
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
|
// 给JSON数据添加操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client version
|
|
|
|
|
// 给JSON数据添加客户端版本号
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
// 发送POST请求,获取服务器响应
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 从响应中获取创建任务列表的结果
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
// 把服务器返回的列表ID设置到本地任务列表对象中
|
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// JSON解析失败,打印日志并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提交待处理的更新操作
|
|
|
|
|
* 通俗说:把mUpdateArray里存储的批量更新操作(修改/新增任务)一次性提交给服务器
|
|
|
|
|
* @throws NetworkFailureException 网络失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
// 如果有未提交的更新操作
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
try {
|
|
|
|
|
// 创建POST请求的JSON数据
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 给JSON数据添加更新操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 给JSON数据添加客户端版本号
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求,提交更新
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
mUpdateArray = null; // 提交后清空更新列表
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// JSON解析失败,打印日志并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed");
|
|
|
|
|
@ -433,153 +599,230 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加待更新的节点(任务/任务列表)
|
|
|
|
|
* 通俗说:把单个更新操作添加到批量更新列表,累计到10个就自动提交,避免一次性提交太多失败
|
|
|
|
|
* @param node 要更新的节点(任务/任务列表)
|
|
|
|
|
* @throws NetworkFailureException 网络失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (node != null) {
|
|
|
|
|
// too many update items may result in an error
|
|
|
|
|
// set max to 10 items
|
|
|
|
|
// 如果更新列表不为空,且数量超过10个,先提交一次
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果更新列表为空,初始化一个新的JSON数组
|
|
|
|
|
if (mUpdateArray == null)
|
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
|
// 把节点的更新操作添加到更新列表
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移动GTask任务
|
|
|
|
|
* 通俗说:把任务从一个任务列表移动到另一个,或在同一个列表内调整位置
|
|
|
|
|
* @param task 要移动的任务
|
|
|
|
|
* @param preParent 任务原来的父列表
|
|
|
|
|
* @param curParent 任务要移动到的目标列表
|
|
|
|
|
* @throws NetworkFailureException 网络失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
// 先提交之前待处理的更新操作
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建POST请求的JSON数据
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 操作列表
|
|
|
|
|
JSONObject action = new JSONObject(); // 移动任务的操作
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 设置操作类型:移动
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
|
|
|
|
|
// 设置操作ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
|
// 设置要移动的任务ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());
|
|
|
|
|
// 如果在同一个列表内移动,且不是第一个任务,设置上一个任务的ID(用来确定位置)
|
|
|
|
|
if (preParent == curParent && task.getPriorSibling() != null) {
|
|
|
|
|
// put prioring_sibing_id only if moving within the tasklist and
|
|
|
|
|
// it is not the first one
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
|
|
|
|
|
}
|
|
|
|
|
// 设置任务原来的列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
|
|
|
|
|
// 设置任务目标列表的父ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid());
|
|
|
|
|
// 如果在不同列表间移动,设置目标列表ID
|
|
|
|
|
if (preParent != curParent) {
|
|
|
|
|
// put the dest_list only if moving between tasklists
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
|
}
|
|
|
|
|
// 把移动操作添加到操作列表
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
// 给JSON数据添加操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 给JSON数据添加客户端版本号
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求,执行移动操作
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// JSON解析失败,打印日志并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除GTask节点(任务/任务列表)
|
|
|
|
|
* 通俗说:把节点标记为已删除,提交到服务器,完成删除操作
|
|
|
|
|
* @param node 要删除的节点(任务/任务列表)
|
|
|
|
|
* @throws NetworkFailureException 网络失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
// 先提交之前待处理的更新操作
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建POST请求的JSON数据
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 操作列表
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 把节点标记为已删除
|
|
|
|
|
node.setDeleted(true);
|
|
|
|
|
// 把删除操作添加到操作列表
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
// 给JSON数据添加操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 给JSON数据添加客户端版本号
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求,执行删除操作
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
mUpdateArray = null; // 清空更新列表
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// JSON解析失败,打印日志并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取所有GTask任务列表
|
|
|
|
|
* 通俗说:从GTask服务器获取用户的所有任务列表(比如“我的任务”“工作任务”)
|
|
|
|
|
* @return 任务列表的JSON数组
|
|
|
|
|
* @throws NetworkFailureException 网络失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
// 如果未登录,抛出异常
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 创建GET请求
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
// 发送GET请求,获取服务器响应
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// get the task list
|
|
|
|
|
// 获取响应的文本内容
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
// 找到JSON数据的起始和结束位置
|
|
|
|
|
int begin = resString.indexOf(jsBegin);
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
String jsString = null;
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
// 截取JSON数据字符串
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
}
|
|
|
|
|
// 解析JSON数据,获取任务列表数组
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
// 客户端协议异常,打印日志并抛出网络失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("gettasklists: httpget failed");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// IO异常,打印日志并抛出网络失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("gettasklists: httpget failed");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// JSON解析失败,打印日志并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定GTask任务列表的所有任务
|
|
|
|
|
* 通俗说:根据任务列表ID,从服务器获取该列表下的所有任务
|
|
|
|
|
* @param listGid 任务列表ID
|
|
|
|
|
* @return 任务的JSON数组
|
|
|
|
|
* @throws NetworkFailureException 网络失败时抛出
|
|
|
|
|
*/
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
|
// 先提交之前待处理的更新操作
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建POST请求的JSON数据
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 操作列表
|
|
|
|
|
JSONObject action = new JSONObject(); // 获取任务列表的操作
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 设置操作类型:获取所有任务
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
|
|
|
|
|
// 设置操作ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
|
// 设置要获取的任务列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
|
|
|
|
|
// 设置是否获取已删除的任务:false=不获取
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
|
|
|
|
|
// 把获取操作添加到操作列表
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
// 给JSON数据添加操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 给JSON数据添加客户端版本号
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求,获取服务器响应
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 从响应中获取任务数组并返回
|
|
|
|
|
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// JSON解析失败,打印日志并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前同步的谷歌账号
|
|
|
|
|
* 通俗说:返回当前用来和GTask同步的谷歌账号信息
|
|
|
|
|
* @return 同步账号
|
|
|
|
|
*/
|
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
|
return mAccount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置更新数据数组
|
|
|
|
|
* 通俗说:清空待提交的更新操作列表,放弃未提交的更新
|
|
|
|
|
*/
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|