|
|
|
@ -1,62 +1,119 @@
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 定义包名
|
|
|
|
|
=======
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
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; // 引入任务数据模型
|
|
|
|
|
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; // 引入GTask字符串工具类
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity; // 引入笔记偏好活动类
|
|
|
|
|
|
|
|
|
|
// 导入Apache HttpClient相关的类
|
|
|
|
|
import org.apache.http.HttpEntity; // 引入HTTP实体类
|
|
|
|
|
import org.apache.http.HttpResponse; // 引入HTTP响应类
|
|
|
|
|
import org.apache.http.client.ClientProtocolException; // 引入客户端协议异常
|
|
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity; // 引入URL编码表单实体类
|
|
|
|
|
import org.apache.http.client.methods.HttpGet; // 引入HTTP GET请求类
|
|
|
|
|
import org.apache.http.client.methods.HttpPost; // 引入HTTP POST请求类
|
|
|
|
|
import org.apache.http.cookie.Cookie; // 引入Cookie类
|
|
|
|
|
import org.apache.http.impl.client.BasicCookieStore; // 引入基本Cookie存储类
|
|
|
|
|
import org.apache.http.impl.client.DefaultHttpClient; // 引入默认HTTP客户端类
|
|
|
|
|
import org.apache.http.message.BasicNameValuePair; // 引入基本名称值对类
|
|
|
|
|
import org.apache.http.params.BasicHttpParams; // 引入基本HTTP参数类
|
|
|
|
|
import org.apache.http.params.HttpConnectionParams; // 引入HTTP连接参数类
|
|
|
|
|
import org.apache.http.params.HttpParams; // 引入HTTP参数类
|
|
|
|
|
|
|
|
|
|
// 导入JSON相关的类
|
|
|
|
|
import org.json.JSONArray; // 引入JSON数组类
|
|
|
|
|
import org.json.JSONException; // 引入JSON异常类
|
|
|
|
|
import org.json.JSONObject; // 引入JSON对象类
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 引入JDK类
|
|
|
|
|
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输入流类
|
|
|
|
|
import java.util.zip.Inflater; // 引入解压缩类
|
|
|
|
|
import java.util.zip.InflaterInputStream; // 引入解压缩输入流类
|
|
|
|
|
|
|
|
|
|
// GTaskClient类负责处理与Google任务(GTask)的通信
|
|
|
|
|
=======
|
|
|
|
|
// GTaskClient类用于与Google Tasks进行交互,实现诸如登录、任务和任务列表的创建、更新、移动、删除以及获取相关数据等功能
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
// 日志标签
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// Google任务相关的URL
|
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
|
=======
|
|
|
|
|
// Google Tasks的基础URL
|
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
|
// 用于获取Google Tasks数据的URL
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
|
// 用于向Google Tasks发送POST请求的URL
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
|
|
|
|
|
|
// 单例实例
|
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// HTTP客户端
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
|
|
|
|
|
// GET和POST请求的URL
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
|
|
// 客户端版本和登录状态相关的字段
|
|
|
|
|
private long mClientVersion;
|
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
private int mActionId;
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
// 更新数组
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
// 私有构造函数,初始化类的成员变量
|
|
|
|
|
=======
|
|
|
|
|
// 用于发送HTTP请求的HttpClient对象
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
// 获取数据的具体URL
|
|
|
|
@ -77,6 +134,7 @@ public class GTaskClient {
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
// 私有构造函数,初始化相关成员变量
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
private GTaskClient() {
|
|
|
|
|
mHttpClient = null;
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
@ -89,7 +147,11 @@ public class GTaskClient {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 获取单例实例
|
|
|
|
|
=======
|
|
|
|
|
// 获取GTaskClient的单例实例
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
@ -97,62 +159,96 @@ public class GTaskClient {
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 登录方法
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
// 假设cookie在5分钟后过期
|
|
|
|
|
=======
|
|
|
|
|
// 执行登录操作
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
// 假设Cookie在5分钟后过期,若距离上次登录时间超过5分钟,则需要重新登录
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
mLoggedin = false; // 如果超时,设置未登录
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 如果账户切换,需要重新登录
|
|
|
|
|
if (mLoggedin && !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity.getSyncAccountName(activity))) {
|
|
|
|
|
mLoggedin = false; // 账户切换,重新登录
|
|
|
|
|
=======
|
|
|
|
|
// 如果已登录,但当前账户与设置中的同步账户不一致,也需要重新登录
|
|
|
|
|
if (mLoggedin
|
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 若已经登录,无需再次登录
|
|
|
|
|
if (mLoggedin) {
|
|
|
|
|
Log.d(TAG, "already logged in");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis(); // 更新最后登录时间
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false); // 获取Google账户的认证令牌
|
|
|
|
|
=======
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis();
|
|
|
|
|
// 登录Google账户获取授权令牌
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false);
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false;
|
|
|
|
|
return false; // 登录失败
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 如果为自定义域,则尝试进行登录
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase().endsWith("googlemail.com"))) {
|
|
|
|
|
=======
|
|
|
|
|
// 如果账户不是以gmail.com或googlemail.com结尾(可能是自定义域名),则使用自定义域名相关的URL进行登录
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
|
.endsWith("googlemail.com"))) {
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
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";
|
|
|
|
|
mGetUrl = url.toString() + "ig"; // 更新GET请求的URL
|
|
|
|
|
mPostUrl = url.toString() + "r/ig"; // 更新POST请求的URL
|
|
|
|
|
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
mLoggedin = true; // 登录成功
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 尝试以 Google 官方 URL 登录
|
|
|
|
|
=======
|
|
|
|
|
// 如果使用自定义域名登录失败,则尝试使用Google官方URL登录
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
|
mGetUrl = GTASK_GET_URL; // 恢复默认的GET请求URL
|
|
|
|
|
mPostUrl = GTASK_POST_URL; // 恢复默认的POST请求URL
|
|
|
|
|
if (!tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
return false;
|
|
|
|
|
return false; // 登录失败
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
mLoggedin = true; // 设置为已登录
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 获取Google账户认证令牌的方法
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
String authToken;
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity); // 获取账户管理器
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google"); // 获取Google账户
|
|
|
|
|
=======
|
|
|
|
|
// 登录Google账户获取授权令牌
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
String authToken;
|
|
|
|
@ -160,15 +256,23 @@ public class GTaskClient {
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
// 获取所有Google类型的账户
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
|
|
|
|
|
// 如果没有可用的Google账户,日志记录并返回null
|
|
|
|
|
if (accounts.length == 0) {
|
|
|
|
|
Log.e(TAG, "there is no available google account");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity); // 获取同步的账户名
|
|
|
|
|
=======
|
|
|
|
|
// 获取设置中的同步账户名称
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
Account account = null;
|
|
|
|
|
|
|
|
|
|
// 找到设置中与账户名匹配的账户
|
|
|
|
|
for (Account a : accounts) {
|
|
|
|
|
if (a.name.equals(accountName)) {
|
|
|
|
|
account = a;
|
|
|
|
@ -176,49 +280,78 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (account != null) {
|
|
|
|
|
mAccount = account;
|
|
|
|
|
mAccount = account; // 设置账户
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "unable to get an account with the same name in the settings");
|
|
|
|
|
return null;
|
|
|
|
|
return null; // 找不到匹配账户,返回null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 获取认证令牌
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account, "goanna_mobile", null, activity, null, null);
|
|
|
|
|
=======
|
|
|
|
|
// 获取授权令牌
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
|
"goanna_mobile", null, activity, null, null);
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
try {
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult(); // 获取结果
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); // 读取令牌
|
|
|
|
|
|
|
|
|
|
// 如果需要无效化令牌,则重新获取
|
|
|
|
|
if (invalidateToken) {
|
|
|
|
|
// 如果需要使令牌失效,则先失效再重新获取
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
|
loginGoogleAccount(activity, false); // 递归调用获取新令牌
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "get auth token failed");
|
|
|
|
|
authToken = null;
|
|
|
|
|
authToken = null; // 获取token失败
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authToken;
|
|
|
|
|
return authToken; // 返回令牌
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 尝试登录到GTask
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
// 如果认证令牌过期,则无效化并尝试重新登录
|
|
|
|
|
=======
|
|
|
|
|
// 尝试登录Google Tasks
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
// 如果登录失败,可能是授权令牌过期,先使令牌失效再重新尝试登录Google账户获取新令牌,然后再次登录Google Tasks
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false;
|
|
|
|
|
return false; // 登录失败
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
Log.e(TAG, "login gtask failed");
|
|
|
|
|
return false;
|
|
|
|
|
return false; // 登录失败
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
return true; // 登录成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 使用认证令牌登录到GTask
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
int timeoutConnection = 10000; // 连接超时设置
|
|
|
|
|
int timeoutSocket = 15000; // Socket超时设置
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams(); // 创建基本HTTP参数
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // 设置连接超时
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); // 设置Socket超时
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters); // 创建HTTP客户端
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore(); // 创建Cookie存储
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore); // 设置Cookie存储
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false); // 设置HTTP协议参数
|
|
|
|
|
|
|
|
|
|
// 登录GTask
|
|
|
|
|
=======
|
|
|
|
|
// 实际执行登录Google Tasks的操作,获取相关信息(如客户端版本号等)
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
@ -233,25 +366,33 @@ public class GTaskClient {
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
// 登录Google Tasks,发送带有授权令牌的GET请求
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
try {
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken; // 创建登录URL
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl); // 创建GET请求
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpGet); // 执行请求
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 获取Cookie
|
|
|
|
|
=======
|
|
|
|
|
// 获取登录后的Cookie信息,检查是否包含认证相关的Cookie(名称包含"GTL")
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
boolean hasAuthCookie = false; // 验证是否存在认证Cookie
|
|
|
|
|
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的警告
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 从响应中获取客户端版本
|
|
|
|
|
=======
|
|
|
|
|
// 从响应中获取客户端版本号等信息,通过解析返回的JavaScript代码中的相关数据
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
@ -259,194 +400,275 @@ public class GTaskClient {
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
String jsString = null;
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end); // 提取JSON字符串
|
|
|
|
|
}
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
mClientVersion = js.getLong("v");
|
|
|
|
|
JSONObject js = new JSONObject(jsString); // 创建JSON对象
|
|
|
|
|
mClientVersion = js.getLong("v"); // 获取客户端版本
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
return false; // 处理JSON异常,返回登录失败
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
=======
|
|
|
|
|
// 捕获其他所有异常,若发生异常则登录失败
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
|
return false;
|
|
|
|
|
return false; // 处理其他异常,返回登录失败
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
return true; // 登录成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 获取下一个动作ID
|
|
|
|
|
=======
|
|
|
|
|
// 获取下一个操作的ID
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
return mActionId++;
|
|
|
|
|
return mActionId++; // 返回当前ID并递增
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 创建HTTP POST请求
|
|
|
|
|
=======
|
|
|
|
|
// 创建用于发送POST请求的HttpPost对象,并设置相关请求头
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
|
httpPost.setHeader("AT", "1");
|
|
|
|
|
return httpPost;
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl); // 创建POST请求
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // 设置请求头
|
|
|
|
|
httpPost.setHeader("AT", "1"); // 设置自定义请求头
|
|
|
|
|
return httpPost; // 返回POST请求
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 获取HTTP响应内容
|
|
|
|
|
=======
|
|
|
|
|
// 从HTTP实体中获取响应内容,根据内容编码(如gzip、deflate等)进行相应的解压处理
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
String contentEncoding = null; // 声明内容编码
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue(); // 获取内容编码
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
InputStream input = entity.getContent(); // 获取输入流
|
|
|
|
|
// 根据内容编码类型进行处理
|
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
|
input = new GZIPInputStream(entity.getContent());
|
|
|
|
|
input = new GZIPInputStream(entity.getContent()); // 处理GZIP编码
|
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
|
Inflater inflater = new Inflater(true);
|
|
|
|
|
input = new InflaterInputStream(entity.getContent(), inflater);
|
|
|
|
|
input = new InflaterInputStream(entity.getContent(), inflater); // 处理Deflate编码
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(input);
|
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(input); // 创建输入流读取器
|
|
|
|
|
BufferedReader br = new BufferedReader(isr); // 创建缓冲读取器
|
|
|
|
|
StringBuilder sb = new StringBuilder(); // 创建字符串构建器
|
|
|
|
|
|
|
|
|
|
// 持续读取内容
|
|
|
|
|
while (true) {
|
|
|
|
|
String buff = br.readLine();
|
|
|
|
|
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(); // 关闭输入流
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
=======
|
|
|
|
|
// 发送POST请求,将JSON数据发送到Google Tasks服务器,并处理响应返回的JSON数据
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
Log.e(TAG, "please login first"); // 如果未登录,记录错误
|
|
|
|
|
throw new ActionFailureException("not logged in"); // 抛出异常
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
|
HttpPost httpPost = createHttpPost(); // 创建POST请求
|
|
|
|
|
try {
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>(); // 创建参数列表
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString())); // 添加请求参数
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8"); // 创建实体
|
|
|
|
|
httpPost.setEntity(entity); // 设置POST实体
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 执行POST请求并获取响应
|
|
|
|
|
=======
|
|
|
|
|
// 执行POST请求并获取响应,然后解析响应内容为JSONObject返回
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
String jsString = getResponseContent(response.getEntity()); // 获取响应内容
|
|
|
|
|
return new JSONObject(jsString); // 返回响应的JSON对象
|
|
|
|
|
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
throw new NetworkFailureException("postRequest failed"); // 抛出网络失败异常
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
throw new NetworkFailureException("postRequest failed"); // 抛出IO异常
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("unable to convert response content to jsonobject");
|
|
|
|
|
throw new ActionFailureException("unable to convert response content to jsonobject"); // 抛出JSON转换异常
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("error occurs when posting request");
|
|
|
|
|
throw new ActionFailureException("error occurs when posting request"); // 其它异常处理
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 创建任务
|
|
|
|
|
=======
|
|
|
|
|
// 创建一个任务并发送到Google Tasks服务器
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
commitUpdate(); // 提交更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建JSON对象
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建动作列表
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 添加创建动作到动作列表
|
|
|
|
|
=======
|
|
|
|
|
// 将任务的创建操作添加到操作列表中
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 添加动作列表到JSON对象
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 添加客户端版本到JSON对象
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 执行POST请求
|
|
|
|
|
=======
|
|
|
|
|
// 设置客户端版本号
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求并处理响应,获取新创建任务的ID并设置到任务对象中
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_RESULTS).get(0); // 获取返回结果
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); // 设置任务的ID
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed"); // 抛出JSON处理异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 创建任务列表
|
|
|
|
|
=======
|
|
|
|
|
// 创建一个任务列表并发送到Google Tasks服务器
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
commitUpdate(); // 提交更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建JSON对象
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建动作列表
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 添加创建动作到动作列表
|
|
|
|
|
=======
|
|
|
|
|
// 将任务列表的创建操作添加到操作列表中
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 添加动作列表到JSON对象
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 添加客户端版本到JSON对象
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 执行POST请求
|
|
|
|
|
=======
|
|
|
|
|
// 设置客户端版本号
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求并处理响应,获取新创建任务列表的ID并设置到任务列表对象中
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_RESULTS).get(0); // 获取返回结果
|
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); // 设置任务列表的ID
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed"); // 抛出JSON处理异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 提交更新
|
|
|
|
|
=======
|
|
|
|
|
// 提交更新操作,将暂存的更新操作数组发送到服务器
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
// 如果存在更新数组
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建JSON对象
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 添加动作列表到JSON对象
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
// 添加客户端版本到JSON对象
|
|
|
|
|
=======
|
|
|
|
|
// 设置操作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
|
|
|
|
|
// 设置客户端版本号
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
postRequest(jsPost); // 执行POST请求
|
|
|
|
|
mUpdateArray = null; // 清空更新数组
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed");
|
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed"); // 抛出JSON处理异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 添加更新节点
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (node != null) {
|
|
|
|
|
// 更新项目过多可能会导致错误,最多设为10个项目
|
|
|
|
|
=======
|
|
|
|
|
// 添加一个节点的更新操作到暂存的更新操作数组中,限制最多10个更新项
|
|
|
|
|
// 将节点的更新操作添加到更新数组中,如果更新数组中的元素超过10个,则先提交更新
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (node != null) {
|
|
|
|
|
// 过多的更新项可能会导致错误,所以设置最大更新项数量为10个
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
commitUpdate(); // 提交更新
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果更新数组为空,则创建一个新的JSONArray对象用于存放更新操作
|
|
|
|
|
if (mUpdateArray == null)
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
mUpdateArray = new JSONArray(); // 初始化更新数组
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId())); // 将更新的节点添加到数组
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 移动任务
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent) throws NetworkFailureException {
|
|
|
|
|
commitUpdate(); // 提交更新
|
|
|
|
|
=======
|
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
|
// 将节点的更新操作(通过节点获取,操作ID使用当前的操作ID)添加到更新数组中
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
@ -457,11 +679,37 @@ public class GTaskClient {
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建JSON对象
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建动作列表
|
|
|
|
|
JSONObject action = new JSONObject(); // 创建动作对象
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 构建移动动作
|
|
|
|
|
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()); // 设置任务ID
|
|
|
|
|
|
|
|
|
|
// 如果前后父任务相同且任务有前兄弟,则附加前兄弟ID
|
|
|
|
|
if (preParent == curParent && task.getPriorSibling() != null) {
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
|
|
|
|
|
}
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); // 源任务列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); // 目标父任务列表ID
|
|
|
|
|
|
|
|
|
|
// 如果任务从一个任务列表移动到另一个任务列表,则附加目标列表ID
|
|
|
|
|
if (preParent != curParent) {
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
|
}
|
|
|
|
|
actionList.put(action); // 添加动作到动作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 添加动作列表到JSON对象
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本到JSON对象
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost); // 执行POST请求
|
|
|
|
|
=======
|
|
|
|
|
// 操作列表相关设置
|
|
|
|
|
// 设置操作类型为移动任务(对应预定义的移动操作类型常量)
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
@ -493,22 +741,51 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
// 发送POST请求,将包含移动任务操作信息的JSON数据发送到服务器进行处理
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed"); // 抛出JSON处理异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除节点
|
|
|
|
|
=======
|
|
|
|
|
// 如果JSON处理出现异常,抛出操作失败异常,并说明是移动任务时处理JSON对象失败
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除节点(比如任务、任务列表等),通过设置节点的删除标记为true,并将其更新操作发送到服务器来实现删除
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
commitUpdate(); // 提交更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建JSON对象
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建动作列表
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 标记节点为已删除并添加到动作列表
|
|
|
|
|
node.setDeleted(true);
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId())); // 获取节点更新动作并添加到列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 添加动作列表到JSON对象
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本到JSON对象
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost); // 执行POST请求
|
|
|
|
|
mUpdateArray = null; // 清空更新数组
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed"); // 抛出JSON处理异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取任务列表
|
|
|
|
|
=======
|
|
|
|
|
// 操作列表相关设置
|
|
|
|
|
// 将节点标记为已删除
|
|
|
|
|
node.setDeleted(true);
|
|
|
|
@ -533,13 +810,21 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取所有的任务列表信息,前提是已经登录成功,否则会抛出异常提示需要先登录
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
Log.e(TAG, "please login first"); // 如果未登录,记录错误
|
|
|
|
|
throw new ActionFailureException("not logged in"); // 抛出异常
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl); // 创建GET请求
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpGet); // 执行请求
|
|
|
|
|
|
|
|
|
|
// 获取任务列表
|
|
|
|
|
String resString = getResponseContent(response.getEntity()); // 获取响应内容
|
|
|
|
|
=======
|
|
|
|
|
// 创建一个HTTP GET请求对象,用于获取任务列表数据,请求的URL是之前设置好的获取数据的URL
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
@ -548,14 +833,36 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
// 从响应的实体中获取内容,并进行处理,提取出包含任务列表信息的JSON数据部分
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
int begin = resString.indexOf(jsBegin);
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
String jsString = null;
|
|
|
|
|
// 提取JSON字符串
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
}
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
JSONObject js = new JSONObject(jsString); // 创建JSON对象
|
|
|
|
|
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"); // 抛出IO异常
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed"); // 抛出JSON处理异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取指定任务列表
|
|
|
|
|
=======
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
// 从解析后的JSON对象中获取任务列表数组并返回,对应预定义的任务列表的JSON键
|
|
|
|
|
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
|
|
|
|
@ -578,13 +885,37 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据给定的任务列表全局唯一ID,获取该任务列表中的任务信息,需要先提交之前暂存的更新操作
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
commitUpdate(); // 提交更新
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建JSON对象
|
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建动作列表
|
|
|
|
|
JSONObject action = new JSONObject(); // 创建动作对象
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 构建获取所有任务的动作
|
|
|
|
|
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); // 设置列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); // 不获取已删除的任务
|
|
|
|
|
actionList.put(action); // 添加动作到动作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 添加动作列表到JSON对象
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本到JSON对象
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost); // 执行POST请求
|
|
|
|
|
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); // 返回任务数组
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed"); // 抛出JSON处理异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取同步账户
|
|
|
|
|
=======
|
|
|
|
|
// 操作列表相关设置
|
|
|
|
|
// 设置操作类型为获取所有任务(对应预定义的获取所有任务操作类型常量)
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
@ -616,11 +947,20 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前用于同步的账户信息
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
|
return mAccount;
|
|
|
|
|
return mAccount; // 返回账户信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
// 重置更新数组
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
mUpdateArray = null; // 清空更新数组
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
=======
|
|
|
|
|
// 重置更新数组,即将其置为空,一般用于清除之前暂存的更新操作相关数据
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
>>>>>>> a495b394fa4686564cc2bfe7d054eb66276713ae
|
|
|
|
|