|
|
@ -61,47 +61,71 @@ import java.util.zip.Inflater;
|
|
|
|
import java.util.zip.InflaterInputStream;
|
|
|
|
import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* GTaskClient类,用于管理Google任务的客户端
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class GTaskClient {
|
|
|
|
public class GTaskClient {
|
|
|
|
|
|
|
|
// 日志标签
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Google任务的基础URL
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取Google任务的URL
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 提交Google任务的URL
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GTaskClient的单例实例
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 默认的HTTP客户端
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取任务的URL
|
|
|
|
private String mGetUrl;
|
|
|
|
private String mGetUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 提交任务的URL
|
|
|
|
private String mPostUrl;
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 客户端版本号
|
|
|
|
private long mClientVersion;
|
|
|
|
private long mClientVersion;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 登录状态
|
|
|
|
private boolean mLoggedin;
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 上次登录时间
|
|
|
|
private long mLastLoginTime;
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 操作ID
|
|
|
|
private int mActionId;
|
|
|
|
private int mActionId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 账户信息
|
|
|
|
private Account mAccount;
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 待更新的任务数组
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 私有构造函数,初始化GTaskClient的实例
|
|
|
|
|
|
|
|
*/
|
|
|
|
private GTaskClient() {
|
|
|
|
private GTaskClient() {
|
|
|
|
mHttpClient = null;
|
|
|
|
mHttpClient = null; // HTTP客户端
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
mGetUrl = GTASK_GET_URL; // 获取任务的URL
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
mPostUrl = GTASK_POST_URL; // 提交任务的URL
|
|
|
|
mClientVersion = -1;
|
|
|
|
mClientVersion = -1; // 客户端版本号
|
|
|
|
mLoggedin = false;
|
|
|
|
mLoggedin = false; // 登录状态
|
|
|
|
mLastLoginTime = 0;
|
|
|
|
mLastLoginTime = 0; // 上次登录时间
|
|
|
|
mActionId = 1;
|
|
|
|
mActionId = 1; // 操作ID
|
|
|
|
mAccount = null;
|
|
|
|
mAccount = null; // 账户信息
|
|
|
|
mUpdateArray = null;
|
|
|
|
mUpdateArray = null; // 待更新的任务数组
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取GTaskClient的单例实例
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return GTaskClient的单例实例
|
|
|
|
|
|
|
|
*/
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
if (mInstance == null) {
|
|
|
|
if (mInstance == null) {
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
mInstance = new GTaskClient();
|
|
|
@ -109,18 +133,21 @@ public class GTaskClient {
|
|
|
|
return mInstance;
|
|
|
|
return mInstance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 登录Google账户
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param activity 当前Activity
|
|
|
|
|
|
|
|
* @return 登录成功返回true,否则返回false
|
|
|
|
|
|
|
|
*/
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
// 假设cookie在5分钟后过期,需要重新登录
|
|
|
|
// then we need to re-login
|
|
|
|
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
mLoggedin = false;
|
|
|
|
mLoggedin = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// need to re-login after account switch
|
|
|
|
// 如果账户切换,则需要重新登录
|
|
|
|
if (mLoggedin
|
|
|
|
if (mLoggedin && !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity.getSyncAccountName(activity))) {
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
|
|
|
mLoggedin = false;
|
|
|
|
mLoggedin = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -136,9 +163,8 @@ public class GTaskClient {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// login with custom domain if necessary
|
|
|
|
// 如果是自定义域名,则使用自定义域名登录
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase().endsWith("googlemail.com"))) {
|
|
|
|
.endsWith("googlemail.com"))) {
|
|
|
|
|
|
|
|
StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
|
|
|
|
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);
|
|
|
|
String suffix = mAccount.name.substring(index);
|
|
|
@ -151,7 +177,7 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// try to login with google official url
|
|
|
|
// 尝试使用Google官方的URL进行登录
|
|
|
|
if (!mLoggedin) {
|
|
|
|
if (!mLoggedin) {
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
@ -164,6 +190,13 @@ public class GTaskClient {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 登录Google账户
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param activity 当前Activity
|
|
|
|
|
|
|
|
* @param invalidateToken 是否使token无效
|
|
|
|
|
|
|
|
* @return 返回登录成功后的authToken,如果失败返回null
|
|
|
|
|
|
|
|
*/
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
String authToken;
|
|
|
|
String authToken;
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
@ -189,14 +222,14 @@ public class GTaskClient {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the token now
|
|
|
|
// 获取token
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account, "goanna_mobile", null, activity, null, null);
|
|
|
|
"goanna_mobile", null, activity, null, null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
if (invalidateToken) {
|
|
|
|
if (invalidateToken) {
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
|
|
|
|
// 使token无效后再次尝试登录
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
@ -207,10 +240,16 @@ public class GTaskClient {
|
|
|
|
return authToken;
|
|
|
|
return authToken;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 尝试登录Gtask
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param activity 当前Activity
|
|
|
|
|
|
|
|
* @param authToken 要使用的authToken
|
|
|
|
|
|
|
|
* @return 登录成功返回true,否则返回false
|
|
|
|
|
|
|
|
*/
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
// maybe the auth token is out of date, now let's invalidate the
|
|
|
|
// 可能authToken已过期,现在让我们使token失效并重新尝试登录
|
|
|
|
// token and try again
|
|
|
|
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
if (authToken == null) {
|
|
|
|
if (authToken == null) {
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
@ -225,25 +264,35 @@ public class GTaskClient {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 登录GTask
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param authToken 要使用的authToken
|
|
|
|
|
|
|
|
* @return 登录成功返回true,否则返回false
|
|
|
|
|
|
|
|
*/
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置HTTP连接和等待超时参数
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建HttpClient并设置Cookie存储
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
// 登录GTask
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
HttpResponse response = null;
|
|
|
|
HttpResponse response = null;
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
// 获取Cookie
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
@ -255,7 +304,7 @@ public class GTaskClient {
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the client version
|
|
|
|
// 获取客户端版本
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
@ -272,7 +321,7 @@ public class GTaskClient {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
// simply catch all exceptions
|
|
|
|
// 捕获所有异常
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -291,14 +340,24 @@ public class GTaskClient {
|
|
|
|
return httpPost;
|
|
|
|
return httpPost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取HTTP响应内容
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param entity 响应实体对象
|
|
|
|
|
|
|
|
* @return 响应内容字符串
|
|
|
|
|
|
|
|
* @throws IOException 当读取响应内容时出现IO异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
|
|
|
// 获取内容的编码方式
|
|
|
|
String contentEncoding = null;
|
|
|
|
String contentEncoding = null;
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取响应内容的输入流
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
|
|
|
// 根据编码方式解压缩输入流
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
input = new GZIPInputStream(entity.getContent());
|
|
|
|
input = new GZIPInputStream(entity.getContent());
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
@ -311,6 +370,7 @@ public class GTaskClient {
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 逐行读取响应内容并拼接为字符串
|
|
|
|
while (true) {
|
|
|
|
while (true) {
|
|
|
|
String buff = br.readLine();
|
|
|
|
String buff = br.readLine();
|
|
|
|
if (buff == null) {
|
|
|
|
if (buff == null) {
|
|
|
@ -319,208 +379,282 @@ public class GTaskClient {
|
|
|
|
sb = sb.append(buff);
|
|
|
|
sb = sb.append(buff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
// 关闭输入流
|
|
|
|
input.close();
|
|
|
|
input.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 发起POST请求并获取JSON格式的响应
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param js 待发送的JSON对象
|
|
|
|
|
|
|
|
* @return 服务器响应的JSON对象
|
|
|
|
|
|
|
|
* @throws NetworkFailureException 当网络操作失败时抛出此异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
|
|
|
// 检查是否已登录
|
|
|
|
if (!mLoggedin) {
|
|
|
|
if (!mLoggedin) {
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建HTTP POST请求
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 将JSON对象添加到POST请求中的参数中
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
// 执行POST请求
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
|
|
|
// 获取响应内容并转换为JSON对象
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
|
|
|
// 客户端协议异常,通常是因为无效的HTTP协议
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
// IO异常,包括网络连接问题等
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
// JSON解析异常
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new ActionFailureException("unable to convert response content to jsonobject");
|
|
|
|
throw new ActionFailureException("unable to convert response content to jsonobject");
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
// 其他异常
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new ActionFailureException("error occurs when posting request");
|
|
|
|
throw new ActionFailureException("error occurs when posting request");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 创建任务
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param task 待创建的任务对象
|
|
|
|
|
|
|
|
* @throws NetworkFailureException 当网络操作失败时抛出此异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
|
|
|
// 提交更新
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 创建POST请求的JSON对象
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
// 添加任务的创建动作到action_list中
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
// 添加客户端版本号
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
// 发起POST请求
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 处理响应数据
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
// JSON解析异常
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("create task: handling jsonobject failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 创建任务列表
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param tasklist 待创建的任务列表对象
|
|
|
|
|
|
|
|
* @throws NetworkFailureException 当网络操作失败时抛出此异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
|
|
|
// 提交更新
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 创建POST请求的JSON对象
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
// 添加任务列表的创建动作到action_list中
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
// client version
|
|
|
|
// 添加客户端版本号
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
// 发起POST请求
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 处理响应数据
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
// JSON解析异常
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("create tasklist: handling jsonobject failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 提交更新
|
|
|
|
|
|
|
|
* @throws NetworkFailureException 当网络操作失败时抛出此异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
// 添加更新操作数组到action_list中
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
// 添加客户端版本号
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发起POST请求
|
|
|
|
postRequest(jsPost);
|
|
|
|
postRequest(jsPost);
|
|
|
|
mUpdateArray = null;
|
|
|
|
mUpdateArray = null;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
// JSON解析异常
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("commit update: handling jsonobject failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 添加更新节点
|
|
|
|
|
|
|
|
* @param node 待添加的更新节点对象
|
|
|
|
|
|
|
|
* @throws NetworkFailureException 当网络操作失败时抛出此异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
if (node != null) {
|
|
|
|
if (node != null) {
|
|
|
|
// too many update items may result in an error
|
|
|
|
// 处理更新操作数组长度超过限制的情况
|
|
|
|
// set max to 10 items
|
|
|
|
// 最大限制为10个
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (mUpdateArray == null)
|
|
|
|
if (mUpdateArray == null)
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加更新操作到更新操作数组中
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 移动任务到指定的任务列表
|
|
|
|
|
|
|
|
* @param task 待移动的任务对象
|
|
|
|
|
|
|
|
* @param preParent 移动前的父任务列表
|
|
|
|
|
|
|
|
* @param curParent 移动后的父任务列表
|
|
|
|
|
|
|
|
* @throws NetworkFailureException 当网络操作失败时抛出此异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
throws NetworkFailureException {
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
|
|
|
// 提交已有的更新操作
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
// 设置移动操作的参数
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断是否在同一个任务列表内移动,并且不是第一个任务
|
|
|
|
|
|
|
|
// 只在这种情况下添加prior_sibing_id参数
|
|
|
|
if (preParent == curParent && task.getPriorSibling() != null) {
|
|
|
|
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_PRIOR_SIBLING_ID, task.getPriorSibling());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断是否在不同的任务列表之间移动
|
|
|
|
|
|
|
|
// 只在这种情况下添加dest_list参数
|
|
|
|
if (preParent != curParent) {
|
|
|
|
if (preParent != curParent) {
|
|
|
|
// put the dest_list only if moving between tasklists
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actionList.put(action);
|
|
|
|
actionList.put(action);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
// 添加客户端版本号
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发起POST请求
|
|
|
|
postRequest(jsPost);
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
// JSON解析异常
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("move task: handling jsonobject failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 删除节点
|
|
|
|
|
|
|
|
* @param node 待删除的节点对象
|
|
|
|
|
|
|
|
* @throws NetworkFailureException 当网络操作失败时抛出此异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate(); // 提交已有的更新操作
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
// 设置节点删除状态并添加删除操作到操作列表中
|
|
|
|
node.setDeleted(true);
|
|
|
|
node.setDeleted(true);
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 设置操作列表
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
// 设置客户端版本号
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发起POST请求
|
|
|
|
postRequest(jsPost);
|
|
|
|
postRequest(jsPost);
|
|
|
|
mUpdateArray = null;
|
|
|
|
mUpdateArray = null; // 清空更新操作数组
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed"); // 处理JSON对象失败时抛出异常
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取任务列表
|
|
|
|
|
|
|
|
* @return 任务列表的JSONArray
|
|
|
|
|
|
|
|
* @throws NetworkFailureException 当网络操作失败时抛出此异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
if (!mLoggedin) {
|
|
|
|
if (!mLoggedin) {
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发起HttpGet请求获取任务列表
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
HttpResponse response = null;
|
|
|
|
HttpResponse response = mHttpClient.execute(httpGet);
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get the task list
|
|
|
|
// 解析响应内容并获取任务列表
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
@ -543,35 +677,40 @@ public class GTaskClient {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed");
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed"); // 处理JSON对象失败时抛出异常
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取任务列表
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param listGid 列表的唯一标识符
|
|
|
|
|
|
|
|
* @return 任务列表的JSONArray
|
|
|
|
|
|
|
|
* @throws NetworkFailureException 当网络操作失败时抛出此异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate(); // 提交已有的更新操作
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
JSONObject jsPost = new JSONObject(); // 创建请求JSON对象
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
JSONArray actionList = new JSONArray(); // 创建操作列表JSON数组
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
JSONObject action = new JSONObject(); // 创建操作JSON对象
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
// 设置操作类型和其他参数,并将操作对象添加到操作列表中
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
|
|
|
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
|
|
|
|
actionList.put(action);
|
|
|
|
actionList.put(action);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 设置操作列表到请求JSON对象中
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 设置客户端版本号
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
JSONObject jsResponse = postRequest(jsPost); // 发起POST请求并获取响应
|
|
|
|
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
|
|
|
|
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); // 从响应中获取任务列表JSONArray
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed"); // 处理JSON对象失败时抛出异常
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|