|
|
|
@ -62,34 +62,43 @@ import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
// 类名的简写,用于日志输出
|
|
|
|
|
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_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;
|
|
|
|
|
|
|
|
|
|
// HTTP客户端
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
|
|
|
|
|
// 获取和发布任务的URL
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
|
|
// 客户端版本号
|
|
|
|
|
private long mClientVersion;
|
|
|
|
|
|
|
|
|
|
// 登录状态
|
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
|
|
|
|
|
// 上次登录时间
|
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
|
|
|
|
|
// 动作ID
|
|
|
|
|
private int mActionId;
|
|
|
|
|
|
|
|
|
|
// 账户信息
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
// 更新数组
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
// 私有构造函数,防止外部直接创建对象
|
|
|
|
|
private GTaskClient() {
|
|
|
|
|
mHttpClient = null;
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
@ -102,6 +111,7 @@ public class GTaskClient {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取单例对象
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
@ -109,34 +119,37 @@ public class GTaskClient {
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 登录方法
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
|
// then we need to re-login
|
|
|
|
|
// 假设cookie会在5分钟后过期,需要重新登录
|
|
|
|
|
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();
|
|
|
|
|
// 获取Google账户的认证令牌
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false);
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// login with custom domain if necessary
|
|
|
|
|
// 如果账户不是Gmail或Googlemail,则尝试使用自定义域名登录
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
|
.endsWith("googlemail.com"))) {
|
|
|
|
|
StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
|
|
|
|
@ -146,12 +159,13 @@ public class GTaskClient {
|
|
|
|
|
mGetUrl = url.toString() + "ig";
|
|
|
|
|
mPostUrl = url.toString() + "r/ig";
|
|
|
|
|
|
|
|
|
|
// 尝试使用自定义域名登录Google任务
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to login with google official url
|
|
|
|
|
// 如果登录失败,则尝试使用Google官方URL登录
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
@ -160,426 +174,502 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 登录成功
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取Google账户的认证令牌
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
String authToken;
|
|
|
|
|
// 获取账户管理器
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
// 获取所有Google账户
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
|
|
|
|
|
|
// 如果没有Google账户,则返回null
|
|
|
|
|
if (accounts.length == 0) {
|
|
|
|
|
Log.e(TAG, "there is no available google account");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取同步账户的名称
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
|
|
|
|
|
Account account = null;
|
|
|
|
|
// 查找与设置中账户名称相同的账户
|
|
|
|
|
for (Account a : accounts) {
|
|
|
|
|
if (a.name.equals(accountName)) {
|
|
|
|
|
account = a;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 如果找到账户,则保存账户信息
|
|
|
|
|
if (account != null) {
|
|
|
|
|
mAccount = account;
|
|
|
|
|
} else {
|
|
|
|
|
// 如果没有找到账户,则返回null
|
|
|
|
|
Log.e(TAG, "unable to get an account with the same name in the settings");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the token now
|
|
|
|
|
// 获取认证令牌
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
|
"goanna_mobile", null, activity, null, null);
|
|
|
|
|
try {
|
|
|
|
|
// 获取认证令牌的Bundle对象
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
|
// 获取认证令牌
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
// 如果需要,则使令牌无效并重新登录
|
|
|
|
|
if (invalidateToken) {
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 如果获取认证令牌失败,则返回null
|
|
|
|
|
Log.e(TAG, "get auth token failed");
|
|
|
|
|
authToken = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authToken;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
// 尝试使用给定的认证令牌登录Google任务
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
|
// 如果使用给定的认证令牌登录失败
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
// 可能认证令牌已过期,现在使令牌无效并再次尝试登录
|
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
|
// 如果获取认证令牌失败
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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) {
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the client version
|
|
|
|
|
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");
|
|
|
|
|
// 如果使用新的认证令牌登录失败
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
Log.e(TAG, "login gtask failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
return mActionId++;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
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);
|
|
|
|
|
// 使用给定的认证令牌登录Google任务
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
// 设置连接超时和套接字超时的时间
|
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
|
// 创建HTTP客户端
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
|
// 创建本地Cookie存储
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
|
// 设置HTTP协议参数
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
// 登录Google任务
|
|
|
|
|
try {
|
|
|
|
|
// 构建登录URL
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
// 执行HTTP GET请求
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// 获取Cookie
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
// 检查是否存在认证Cookie
|
|
|
|
|
if (cookie.getName().contains("GTL")) {
|
|
|
|
|
hasAuthCookie = true;
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
input.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
if (!hasAuthCookie) {
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
|
try {
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
|
// 获取客户端版本
|
|
|
|
|
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) {
|
|
|
|
|
// 捕获所有异常
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
// 获取动作ID
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
// 返回并递增动作ID
|
|
|
|
|
return mActionId++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// post
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
// 创建HTTP POST请求
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
// 获取HTTP响应的内容
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
// 获取内容编码
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
try {
|
|
|
|
|
// 读取响应内容
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(input);
|
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed");
|
|
|
|
|
while (true) {
|
|
|
|
|
String buff = br.readLine();
|
|
|
|
|
if (buff == null) {
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
sb = sb.append(buff);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
// 关闭输入流
|
|
|
|
|
input.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (node != null) {
|
|
|
|
|
// too many update items may result in an error
|
|
|
|
|
// set max to 10 items
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mUpdateArray == null)
|
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
}
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
// 如果未登录,则抛出异常
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
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_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());
|
|
|
|
|
}
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
// 创建HTTP POST请求
|
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
|
try {
|
|
|
|
|
// 创建参数列表
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
|
// 创建URL编码的表单实体
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
// 执行POST请求
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
// 获取响应内容
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
// 将响应内容转换为JSON对象
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
// 创建任务
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
// 提交更新
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// 添加创建任务的动作
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// 设置客户端版本
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 获取响应结果
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
// 设置任务的GID
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
// 创建任务列表
|
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
// 提交更新
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// 添加创建任务列表的动作
|
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// 设置客户端版本
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 获取响应结果
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
// 设置任务列表的GID
|
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
// 提交更新
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
// 如果存在更新数组
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
try {
|
|
|
|
|
// 创建JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
node.setDeleted(true);
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
// 添加更新动作列表
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 设置客户端版本
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
// 清空更新数组
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
// 添加更新节点
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
// 如果节点不为空
|
|
|
|
|
if (node != null) {
|
|
|
|
|
// 如果更新项过多,则提交更新
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
// 如果更新数组为空,则创建新的数组
|
|
|
|
|
if (mUpdateArray == null)
|
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
|
// 添加更新动作
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 移动任务
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
// 提交更新
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// 添加移动任务的动作
|
|
|
|
|
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) {
|
|
|
|
|
// 如果在同一个任务列表中移动且不是第一个,则添加前一个兄弟节点的ID
|
|
|
|
|
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) {
|
|
|
|
|
// 如果在不同任务列表之间移动,则添加目标列表的ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
|
}
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// 设置客户端版本
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
// 删除节点
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
// 提交所有挂起的更新
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建JSON对象来构建请求体
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// 设置节点为已删除,并添加更新动作到动作列表中
|
|
|
|
|
node.setDeleted(true);
|
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// 设置客户端版本号
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
// 清空更新数组
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
// 获取任务列表
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
// 如果未登录,则抛出异常
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
|
return mAccount;
|
|
|
|
|
try {
|
|
|
|
|
// 创建HTTP GET请求
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// 获取任务列表
|
|
|
|
|
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);
|
|
|
|
|
// 返回任务列表的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");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
// 获取特定任务列表中的任务
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
|
// 提交所有挂起的更新
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建JSON对象来构建请求体
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// 构建获取任务的动作
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 设置客户端版本号
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送请求并获取响应
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 返回任务的JSON数组
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取用于同步的账户
|
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
|
return mAccount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重置更新数组
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|