|
|
@ -0,0 +1,767 @@
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @ProiectName:MiNote
|
|
|
|
|
|
|
|
* @Package: gtask.remote
|
|
|
|
|
|
|
|
* @ClassName: GTaskAClient
|
|
|
|
|
|
|
|
* @Description: 主要实现GTask客户端,提供了与Google Tasks API进行交互的一系列方法,使得小米便签能够使用Google Tasks作为后端任务管理服务。
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
|
|
|
|
//以下内容为引入Android中的各个类
|
|
|
|
|
|
|
|
import android.accounts.Account;//引入Android中的Account类,代表一个用户账户
|
|
|
|
|
|
|
|
import android.accounts.AccountManager;//引入Android中的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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: GTaskClient
|
|
|
|
|
|
|
|
* @功能描述: GTaskClient类的成员变量声明
|
|
|
|
|
|
|
|
* @实现过程: 成员变量在类的方法中被使用和操作,用于存储和处理GTaskClient的状态和数据。
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();//TAG是一个常量字符串,用于在日志输出中标识GTaskClient类。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private long mClientVersion;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int mActionId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: GTaskClient()
|
|
|
|
|
|
|
|
* @功能描述: 私有的构造函数,用于创建GTaskClient类的实例
|
|
|
|
|
|
|
|
* @实现过程: 将各个变量分别初始化,表示初始状态
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private GTaskClient() {
|
|
|
|
|
|
|
|
mHttpClient = null; //将mHttpClient初始化为null,表示初始情况下没有可用的HTTP客户端。
|
|
|
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
|
|
|
|
mClientVersion = -1; //将mClientVersion初始化为-1,表示客户端版本号未知。
|
|
|
|
|
|
|
|
mLoggedin = false; //将mLoggedin初始化为false,表示当前未登录状态。
|
|
|
|
|
|
|
|
mLastLoginTime = 0; //将mLastLoginTime初始化为0,表示上次登录时间为初始状态。
|
|
|
|
|
|
|
|
mActionId = 1;
|
|
|
|
|
|
|
|
mAccount = null;
|
|
|
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名:getInstance
|
|
|
|
|
|
|
|
* @参数 :mInstance
|
|
|
|
|
|
|
|
* @功能描述: 用来获取的实例化对象
|
|
|
|
|
|
|
|
* @实现过程: 使用getInstance(),返回mInstance这个实例化对
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return mInstance;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: login
|
|
|
|
|
|
|
|
* @参数 : activity
|
|
|
|
|
|
|
|
* @功能描述: 实现了对Google Tasks服务的登录操作,包括对登录状态的判断、获取授权Token和根据账户类型选择不同的登录URL等逻辑
|
|
|
|
|
|
|
|
* @实现过程: 定义了一个间隔时间,检查上次登录的时间加上间隔时间是否已经超过了当前时间
|
|
|
|
|
|
|
|
* 检查如果已经登录,当前同步的账户与之前保存的账户是否一致,
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
|
|
|
|
// then we need to re-login
|
|
|
|
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// need to re-login after account switch
|
|
|
|
|
|
|
|
if (mLoggedin
|
|
|
|
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mLoggedin) {
|
|
|
|
|
|
|
|
Log.d(TAG, "already logged in");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false);//进行账户登录,获取授权Token。
|
|
|
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// login with custom domain if necessary
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
String suffix = mAccount.name.substring(index);
|
|
|
|
|
|
|
|
url.append(suffix + "/");
|
|
|
|
|
|
|
|
mGetUrl = url.toString() + "ig";
|
|
|
|
|
|
|
|
mPostUrl = url.toString() + "r/ig";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// try to login with google official url
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
*条件判断和逻辑是根据账户类型(是否是自定义域名或者google官方账户)来确定登录的URL,
|
|
|
|
|
|
|
|
*并尝试进行登录操作。如果登录成功,则将mLoggedin设置为true,表示登录成功。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
|
|
|
|
if (!tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mLoggedin = true;//将登录状态设置为true,并返回true表示登录成功。
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: loginGoogleAccount
|
|
|
|
|
|
|
|
* @参数 : activity(对象)/invalidateToken
|
|
|
|
|
|
|
|
* @功能描述: 用于获取Google账户的授权Token
|
|
|
|
|
|
|
|
* @实现过程: 首先,获取系统的AccountManager对象,用于管理账户信息。
|
|
|
|
|
|
|
|
* 然后,获取所有类型为"com.google"的账户。
|
|
|
|
|
|
|
|
* 如果没有找到任何Google账户,就会打印错误日志并返回null。
|
|
|
|
|
|
|
|
* 接下来,从应用的偏好设置中获取要同步的账户名称。
|
|
|
|
|
|
|
|
* 然后,遍历所有账户,找到与要同步的账户名称匹配的账户对象。
|
|
|
|
|
|
|
|
* 如果找到了匹配的账户,将其赋值给mAccount变量;否则,打印错误日志并返回null。
|
|
|
|
|
|
|
|
* 接着,调用getAuthToken方法获取指定账户的授权Token
|
|
|
|
|
|
|
|
* 通过调用accountManagerFuture.getResult()方法获取授权Token,并将其保存在authToken变量中。
|
|
|
|
|
|
|
|
* 递归调用loginGoogleAccount(activity, false)方法重新获取新的授权Token。
|
|
|
|
|
|
|
|
* 如果获取授权Token的过程中发生异常,就会打印错误日志,并将authToken设置为null。
|
|
|
|
|
|
|
|
* 最后,返回获取到的授权Token。
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
|
|
|
String authToken;
|
|
|
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);//获取系统对象
|
|
|
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");//获取类型为"com.google"账户
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
//"goanna_mobile"是用于访问Google服务的令牌类型
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
|
|
|
if (invalidateToken) {
|
|
|
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) { //捕获可能发生的异常
|
|
|
|
|
|
|
|
Log.e(TAG, "get auth token failed");//打印错误信息
|
|
|
|
|
|
|
|
authToken = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return authToken;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: tryToLoginGtask
|
|
|
|
|
|
|
|
* @参数 : activity(对象) / authToken
|
|
|
|
|
|
|
|
* @功能描述: 尝试使用给定的授权Token登录Google任务(GTask)服务
|
|
|
|
|
|
|
|
* @实现过程: tryToLoginGtask方法首先调用loginGtask方法,尝试使用给定的授权Token登录GTask服务。如果登录
|
|
|
|
|
|
|
|
* 失败,就说明可能授权Token已经过期或无效了,此时需要尝试重新获取一个新的授权Token,并再次尝试登录GTask服务。
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
|
|
|
Log.e(TAG, "login gtask failed");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: loginGtask
|
|
|
|
|
|
|
|
* @参数 : authToken
|
|
|
|
|
|
|
|
* @功能描述: 登录Google任务(GTask)服务的逻辑
|
|
|
|
|
|
|
|
* @实现过程: 首先,代码设置了连接和套接字的超时时间
|
|
|
|
|
|
|
|
* 然后,通过拼接授权Token到登录URL上,
|
|
|
|
|
|
|
|
* 接下来,代码从响应中获取Cookie,检查是否存在名为"GTL"的认证Cookie
|
|
|
|
|
|
|
|
* 最后代码从响应内容中提取客户端版本信息
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//创建了一个DefaultHttpClient对象,并设置了一些Http参数
|
|
|
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
|
|
|
|
//创建一个HttpGet对象,并使用mHttpClient.execute(httpGet)发送请求,获取响应
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
|
|
|
if (cookie.getName().contains("GTL")) {
|
|
|
|
|
|
|
|
hasAuthCookie = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasAuthCookie) { //检查是否存在名为"GTL"的认证Cookie
|
|
|
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get the client version
|
|
|
|
|
|
|
|
//查找特定字符串来获取JavaScript代码块,并将其解析为JSON对象,从中提取客户端版本号
|
|
|
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
|
|
|
int begin = resString.indexOf(jsBegin);
|
|
|
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
|
|
|
String jsString = null;
|
|
|
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
|
|
|
mClientVersion = js.getLong("v");
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
return false; //执行过程出现了异常
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
// simply catch all exceptions
|
|
|
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");//打印错误信息
|
|
|
|
|
|
|
|
return false; //执行过程出现了异常
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true; //返回true表示登录成功
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: getActionId
|
|
|
|
|
|
|
|
* @功能描述: 获取一个序列号,为每个操作分配一个唯一的标识符
|
|
|
|
|
|
|
|
* @实现过程: 每次调用getActionId()方法时,都会将内部成员变量mActionId的值加1,并返回增加后的值
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
|
|
|
return mActionId++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: createHttpPost
|
|
|
|
|
|
|
|
* @功能描述: 创建HttpPost对象,并设置一些请求头
|
|
|
|
|
|
|
|
* @实现过程: 将HttpPost的构造方法中传递的URL设置为成员变量mPostUrl
|
|
|
|
|
|
|
|
* 然后,设置了一个名为Content-Type的请求头,最后返回httpPost
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
|
|
|
|
httpPost.setHeader("AT", "1"); //设置了一个名为AT的请求头,并将值设置为1
|
|
|
|
|
|
|
|
return httpPost;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: getResponseContent
|
|
|
|
|
|
|
|
* @参数 : HttpEntity entity
|
|
|
|
|
|
|
|
* @功能描述: 从HttpEntity对象中获取响应内容,并将其转换为字符串格式。
|
|
|
|
|
|
|
|
* @实现过程: 检查HttpEntity的内容编码,创建了一个InputStreamReader和一个BufferedReader,用于逐行读取响应内容。
|
|
|
|
|
|
|
|
* 逐行读取响应内容并拼接成完整的字符串。最后,返回拼接后的字符串作为响应内容
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 如果存在内容编码,则根据内容编码的值创建相应的输入流。如果内容编码是"gzip",则使用GZIPInputStream来解压缩输入流;
|
|
|
|
|
|
|
|
* 如果内容编码是"deflate",则使用InflaterInputStream来解压缩输入流。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
|
|
|
|
input = new GZIPInputStream(entity.getContent());
|
|
|
|
|
|
|
|
} 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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
String buff = br.readLine();
|
|
|
|
|
|
|
|
if (buff == null) {
|
|
|
|
|
|
|
|
return sb.toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//返回拼接后的字符串作为响应内容
|
|
|
|
|
|
|
|
sb = sb.append(buff);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
input.close();//在处理完响应内容后,确保关闭了输入流,以释放资源。
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: postRequest
|
|
|
|
|
|
|
|
* @参数 : JSONObject js
|
|
|
|
|
|
|
|
* @功能描述: 向服务器发送POST请求并获取响应,返回一个JSONObject对象,从而进一步处理或显示给用户
|
|
|
|
|
|
|
|
* @实现过程: 首先,检查用户是否已登录;接着,创建一个HttpPost对象,然后,它将JSON格式的数据放入一个
|
|
|
|
|
|
|
|
* 0BasicNameValuePair列表中,最后执行POST请求,获取服务器返回的响应实体
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
|
|
|
Log.e(TAG, "please login first");//如果没有登录,则抛出ActionFailureException异常,并提示用户需要先登录
|
|
|
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpPost httpPost = createHttpPost();//创建一个HttpPost对象
|
|
|
|
|
|
|
|
try {//设置请求头等参数
|
|
|
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
//将JSON格式的数据放入一个BasicNameValuePair列表中,并将列表转换为UrlEncodedFormEntity实体。
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());//将响应实体转换为字符串格式
|
|
|
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (ClientProtocolException e) {//如果发生错误,则打印出对应的问题
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: createTask
|
|
|
|
|
|
|
|
* @参数 : Task task
|
|
|
|
|
|
|
|
* @功能描述: 创建一个任务并将其发送到服务器进行处理
|
|
|
|
|
|
|
|
* @实现过程: 调用commitUpdate()方法,创建一个JSONObject对象,然后,它调用postRequest(jsPost)方法向服务器发送POST请求,
|
|
|
|
|
|
|
|
* 并获取服务器返回的响应。最后,从响应中获取结果,并将结果中的新任务ID设置到task对象中
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
|
|
|
commitUpdate();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
|
|
|
//将actionList放入jsPost中,并添加客户端版本信息
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);//向服务器发送POST请求,并获取服务器返回的响应
|
|
|
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) { //如果在处理过程中出现JSONException异常,则会捕获并打印日志
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: createTaskList.NetworkFailureException
|
|
|
|
|
|
|
|
* @参数 : TaskList tasklist
|
|
|
|
|
|
|
|
* @功能描述: 创建一个任务列表并将其发送到服务器进行处理
|
|
|
|
|
|
|
|
* @实现过程: 调用commitUpdate()方法, 创建一个JSONObject对象, 然后,调用tasklist.getCreateAction(getActionId())
|
|
|
|
|
|
|
|
* 方法获取任务列表的创建操作,并添加客户端版本信息。最后,从响应中获取结果,并将结果中的新任务ID设置到tasklist中
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
|
|
|
commitUpdate();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// client version
|
|
|
|
|
|
|
|
//将actionList放入jsPost中,并添加客户端版本信息
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
|
|
|
//调用postRequest(jsPost)方法向服务器发送POST请求,并获取服务器返回的响应
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) { //处理过程中出现JSONException异常,则会捕获并打印日志,并抛出ActionFailureException异常,表示处理JSON对象失败
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: NetworkFailureException
|
|
|
|
|
|
|
|
* @功能描述: 提交之前的任务更新操作
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 如果mUpdateArray不为空,表示有待提交的任务更新操作,那么它会创建一个JSONObject对象jsPost,
|
|
|
|
|
|
|
|
* 用于存储要发送给服务器的数据,然后将mUpdateArray放入jsPost中,并添加客户端版本信息
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);//向服务器发送POST请求,将更新的任务数据提交到服务器进行处理
|
|
|
|
|
|
|
|
mUpdateArray = null;//mUpdateArray设置为null,表示已经提交了所有的任务更新操作
|
|
|
|
|
|
|
|
} catch (JSONException e) { //处理过程中出现JSONException异常,则会捕获并打印日志
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: NetworkFailureException
|
|
|
|
|
|
|
|
* @参数 : Node node
|
|
|
|
|
|
|
|
* @功能描述: 添加待提交的任务更新操作
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
|
|
|
if (node != null) {
|
|
|
|
|
|
|
|
// too many update items may result in an error
|
|
|
|
|
|
|
|
// set max to 10 items
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 如果传入的node不为空,它首先检查mUpdateArray是否为空,并且其中的任务更新操作数量是否超过了最大值,
|
|
|
|
|
|
|
|
* 如果超过了最大值,那么它会调用commitUpdate()方法提交之前的任务更新操作。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
|
|
|
|
commitUpdate();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mUpdateArray == null)//如果mUpdateArray仍然为空,那么它会创建一个JSONArray对象mUpdateArray,用于存储任务更新操作
|
|
|
|
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));//获取节点的更新操作,并将其添加到mUpdateArray中
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: moveTask
|
|
|
|
|
|
|
|
* @参数 : task, preParent, curParent
|
|
|
|
|
|
|
|
* @功能描述: 将一个任务从一个任务列表移动到另一个任务列表
|
|
|
|
|
|
|
|
* @实现过程: 保存修改,然后存储要发送给服务器的数据,最后,它将客户端版本信息添加到jsPost中
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 它调用commitUpdate()方法提交之前的任务更新操作,以确保之前未提交的更新操作被保存
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
|
|
|
commitUpdate();
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 创建一个JSONArray对象actionList,用于存储要执行的操作
|
|
|
|
|
|
|
|
* 创建一个JSONObject对象action,表示要执行的移动操作
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
|
|
|
//在action中,它设置了以下属性
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid());
|
|
|
|
|
|
|
|
if (preParent != curParent) {
|
|
|
|
|
|
|
|
// put the dest_list only if moving between tasklists
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//将action添加到actionList中,并将actionList添加到jsPost中
|
|
|
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) { //如果在处理过程中出现JSONException异常,则会捕获并打印日志
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: deleteNode
|
|
|
|
|
|
|
|
* @参数 : node
|
|
|
|
|
|
|
|
* @功能描述: 删除一个节点(Node)
|
|
|
|
|
|
|
|
* @实现过程: 首先,确保之前未提交的更新操作被保存,然后,创建一个JSONObject对象,存储要发送给服务器的数据
|
|
|
|
|
|
|
|
* 然后,它将客户端版本信息添加到jsPost中,最后,它将mUpdateArray设置为null,清空之前的更新操作列表
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
|
|
|
commitUpdate();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 它将node对象的deleted属性设置为true,表示该节点将被删除。
|
|
|
|
|
|
|
|
* 调用node.getUpdateAction(getActionId())方法获取表示删除节点操作的JSONObject对象,并将该对象添加到actionList中
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
node.setDeleted(true);
|
|
|
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 将客户端版本信息添加到jsPost中,并调用postRequest(jsPost)方法向服务器发送POST请求,
|
|
|
|
|
|
|
|
* 将删除节点的操作提交到服务器进行处理。如果在处理过程中出现JSONException异常,则会捕获并
|
|
|
|
|
|
|
|
* 打印日志,并抛出ActionFailureException异常,表示处理JSON对象失败
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: getTaskLists
|
|
|
|
|
|
|
|
* @参数 : mLoggedin
|
|
|
|
|
|
|
|
* @功能描述: 获取任务列表(Task Lists)
|
|
|
|
|
|
|
|
* @实现过程: 首先,检查是否已经登录。接下来,它创建一个HttpGet对象,用于发送GET请求,并将响应结果存储在HttpResponse对象中
|
|
|
|
|
|
|
|
* 然后,它从响应结果中提取任务列表的相关信息。它通过字符串的处理,找到包含任务列表信息的部分,并将其转换为一个JSONObject对象js
|
|
|
|
|
|
|
|
* 最后,它从js对象中获取键为"t"的子对象,再从该子对象中获取键为GTaskStringUtils.GTASK_JSON_LISTS的子数组,并将其作为结果返回
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get the task list
|
|
|
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
|
|
|
int begin = resString.indexOf(jsBegin);
|
|
|
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
|
|
|
String jsString = null;
|
|
|
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
throw new NetworkFailureException("gettasklists: httpget failed");
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: getTaskList
|
|
|
|
|
|
|
|
* @参数 :listGid
|
|
|
|
|
|
|
|
* @功能描述: 获取指定任务列表(Task List)的任务信息
|
|
|
|
|
|
|
|
* @实现过程: 首先保存更新操作,存储要发送给服务器的数据,接着,它将客户端版本信息添加到jsPost中,发送POST请求
|
|
|
|
|
|
|
|
* 最后,它从服务器返回的响应结果中获取键为"tasks"的子数组,并将其作为结果返回
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
|
|
|
|
commitUpdate();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
JSONObject jsPost = 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);
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
|
|
|
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);//将actionList添加到jsPost中
|
|
|
|
|
|
|
|
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
|
|
|
|
|
|
|
|
//将客户端版本信息添加到jsPost中,并调用postRequest(jsPost)方法向服务器发送POST请求,将获取任务列表的操作提交到服务器进行处理。
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: getSyncAccount
|
|
|
|
|
|
|
|
* @功能描述:返回当前同步帐户
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
|
|
|
|
return mAccount;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @功能名: resetUpdateArray
|
|
|
|
|
|
|
|
* @功能描述: 重置更新数组
|
|
|
|
|
|
|
|
* @实现过程: resetUpdateArray()方法将类中的mUpdateArray成员变量设置为null,以清空已有的更新数组内容。
|
|
|
|
|
|
|
|
* 这样做可以确保在处理下一批更新操作时,不会受到之前更新的影响,从而保持更新操作的独立性和准确性。
|
|
|
|
|
|
|
|
* @Author: wmq
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|