|
|
|
@ -1,401 +0,0 @@
|
|
|
|
/*
|
|
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GTaskClient类主要负责与Google Tasks服务进行交互,包括登录验证、获取任务列表、创建任务、更新任务等各种操作,是实现与Google Tasks数据同步的核心类之一
|
|
|
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
|
|
|
// 用于日志记录的标签,方便在日志中识别该类相关的输出信息,值为类的简单名称
|
|
|
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
|
|
|
// Google Tasks服务的基础URL
|
|
|
|
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
|
|
|
|
// 用于获取Google Tasks数据的GET请求URL
|
|
|
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
|
|
|
|
// 用于向Google Tasks服务提交数据(如创建、更新等操作)的POST请求URL
|
|
|
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
|
|
|
|
// 采用单例模式,保存GTaskClient的唯一实例
|
|
|
|
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
// 用于发送HTTP请求的HttpClient对象,用于执行与Google Tasks服务的网络通信
|
|
|
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
|
|
|
// 当前用于获取数据的GET请求URL,可能会根据登录情况等因素进行调整
|
|
|
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
|
|
|
// 当前用于提交数据的POST请求URL,可能会根据登录情况等因素进行调整
|
|
|
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
// 客户端版本号,用于与Google Tasks服务交互时标识客户端的版本情况
|
|
|
|
|
|
|
|
private long mClientVersion = -1;
|
|
|
|
|
|
|
|
// 标记当前是否已登录到Google Tasks服务
|
|
|
|
|
|
|
|
private boolean mLoggedin = false;
|
|
|
|
|
|
|
|
// 记录上次登录的时间戳,用于判断是否需要重新登录(例如基于登录有效期等逻辑)
|
|
|
|
|
|
|
|
private long mLastLoginTime = 0;
|
|
|
|
|
|
|
|
// 用于生成每个操作的唯一ID,每次操作后自增,确保每个操作在请求中有唯一标识
|
|
|
|
|
|
|
|
private int mActionId = 1;
|
|
|
|
|
|
|
|
// 关联的Google账户对象,用于登录验证以及与账户相关的操作
|
|
|
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
// 用于存储待提交的更新操作相关的JSON数据数组,批量提交更新时使用
|
|
|
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 私有构造函数,初始化GTaskClient实例的各个成员变量
|
|
|
|
|
|
|
|
private GTaskClient() {
|
|
|
|
|
|
|
|
mHttpClient = null;
|
|
|
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
|
|
|
|
mClientVersion = -1;
|
|
|
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
|
|
|
mLastLoginTime = 0;
|
|
|
|
|
|
|
|
mActionId = 1;
|
|
|
|
|
|
|
|
mAccount = null;
|
|
|
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 静态方法,用于获取GTaskClient的单例实例,如果实例不存在则创建一个新的实例
|
|
|
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return mInstance;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 执行登录到Google Tasks服务的方法,处理登录逻辑,包括判断是否需要重新登录、获取授权令牌、根据账户类型尝试不同的登录方式等
|
|
|
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
|
|
|
// 假设登录的Cookie有效期为5分钟,超过这个时间间隔则需要重新登录
|
|
|
|
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果账户切换了,也需要重新登录
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果账户不是以"gmail.com"或"googlemail.com"结尾,可能需要使用自定义域名登录(针对企业等自定义域名情况)
|
|
|
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
|
|
|
|
.endsWith("googlemail.com"))) {
|
|
|
|
|
|
|
|
StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
|
|
|
|
|
|
|
|
int index = mAccount.name.indexOf('@') + 1;
|
|
|
|
|
|
|
|
String suffix = mAccount.name.substring(index);
|
|
|
|
|
|
|
|
url.append(suffix + "/");
|
|
|
|
|
|
|
|
mGetUrl = url.toString() + "ig";
|
|
|
|
|
|
|
|
mPostUrl = url.toString() + "r/ig";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果使用自定义域名登录失败,尝试使用Google官方的URL进行登录
|
|
|
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
|
|
|
|
if (!tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 登录Google账户获取授权令牌的方法,通过AccountManager查找合适的Google账户,并获取对应的授权令牌
|
|
|
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
|
|
|
String authToken;
|
|
|
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (accounts.length == 0) {
|
|
|
|
|
|
|
|
Log.e(TAG, "there is no available google account");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
|
|
|
|
|
|
|
|
Account account = null;
|
|
|
|
|
|
|
|
for (Account a : accounts) {
|
|
|
|
|
|
|
|
if (a.name.equals(accountName)) {
|
|
|
|
|
|
|
|
account = a;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (account!= null) {
|
|
|
|
|
|
|
|
mAccount = account;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Log.e(TAG, "unable to get an account with the same name in the settings");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取授权令牌
|
|
|
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
|
|
|
|
"goanna_mobile", null, activity, null, null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
|
|
|
if (invalidateToken) {
|
|
|
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
Log.e(TAG, "get auth token failed");
|
|
|
|
|
|
|
|
authToken = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return authToken;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 尝试登录到Google Tasks服务的方法,内部可能会处理授权令牌过期等情况,多次尝试登录操作
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
|
|
|
Log.e(TAG, "login gtask failed");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 实际执行登录到Google Tasks服务的具体操作,发送HTTP GET请求进行登录验证,获取相关的Cookie和客户端版本号等信息
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发送登录的GET请求
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取登录后返回的Cookie,检查是否包含认证相关的Cookie(例如查找名称包含"GTL"的Cookie)
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 从登录响应中获取客户端版本号,通过解析返回的HTML内容(提取其中包含版本号的JSON数据部分)来获取
|
|
|
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
|
|
|
int begin = resString.indexOf(jsBegin);
|
|
|
|
|
|
|
|
int end = resString.lastIndexOf(jsEnd);
|
|
|
|
|
|
|
|
String jsString = null;
|
|
|
|
|
|
|
|
if (begin!= -1 && end!= -1 && begin < end) {
|
|
|
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
|
|
|
mClientVersion = js.getLong("v");
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
// 简单捕获所有可能出现的异常情况,记录日志并返回登录失败
|
|
|
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取下一个操作的唯一ID,每次调用后ID值自增,用于在请求中标识不同的操作
|
|
|
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
|
|
|
return mActionId++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个用于POST请求的HttpPost对象,设置相关的请求头信息(如内容类型、特定的标识等)
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 从给定的HttpEntity中获取响应内容的方法,处理内容的编码(如gzip、deflate等解压情况)并读取内容返回为字符串
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
input.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发送POST请求并将响应内容转换为JSONObject的方法,处理请求发送、响应读取以及JSON解析等操作,若出现问题则抛出相应异常
|
|
|
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 执行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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个新的任务(Task)并提交到Google Tasks服务的方法,构建相应的请求JSON数据,发送请求后获取新任务的唯一ID并设置到任务对象中
|
|
|
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
|
|
|
commitUpdate();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 添加创建任务的操作信息到操作列表中,使用任务对象生成对应的创建操作JSON数据
|
|
|
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置客户端版本号到请求JSON数据中
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发送POST请求并处理响应,获取新任务的ID并设置到任务对象中
|
|
|
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个新的任务列表(TaskList)并提交到Google Tasks服务的方法,与创建任务类似,构建请求JSON数据,发送请求后获取新任务列表的唯一ID并设置到任务列表对象中
|
|
|
|
|
|
|
|
public void createTask
|
|
|
|
|