|
|
|
@ -24,6 +24,23 @@ import android.os.Bundle;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.HttpResponse;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.ClientProtocolException;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.CookieStore;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.config.RequestConfig;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.methods.HttpGet;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.methods.HttpPost;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.protocol.HttpClientContext;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.cookie.Cookie;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.impl.client.BasicCookieStore;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.impl.client.HttpClients;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.message.BasicNameValuePair;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.params.BasicHttpParams;
|
|
|
|
|
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.params.HttpProtocolParams;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.gtask.data.Node;
|
|
|
|
|
import net.micode.notes.gtask.data.Task;
|
|
|
|
|
import net.micode.notes.gtask.data.TaskList;
|
|
|
|
@ -32,33 +49,16 @@ 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.nio.charset.Charset;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
|
import java.util.zip.Inflater;
|
|
|
|
|
import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class GTaskClient {
|
|
|
|
@ -72,7 +72,7 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
private CloseableHttpClient mHttpClient;
|
|
|
|
|
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
|
|
|
|
@ -143,8 +143,8 @@ public class GTaskClient {
|
|
|
|
|
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 + "ig";
|
|
|
|
|
mPostUrl = url + "r/ig";
|
|
|
|
|
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
|
mLoggedin = true;
|
|
|
|
@ -226,25 +226,31 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
// 响应超时的毫秒数
|
|
|
|
|
int timeoutResponse = 15000;
|
|
|
|
|
// 实例HttpClient对象
|
|
|
|
|
mHttpClient= HttpClients.createDefault();
|
|
|
|
|
// 设置请求配置
|
|
|
|
|
RequestConfig requestConfig=RequestConfig.custom()
|
|
|
|
|
.setConnectionRequestTimeout(Timeout.ofMilliseconds(timeoutConnection))
|
|
|
|
|
.setResponseTimeout(Timeout.ofMilliseconds(timeoutResponse))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
|
try {
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
// 创建Get请求
|
|
|
|
|
HttpGet httpGet=new HttpGet(loginUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
httpGet.setConfig(requestConfig);
|
|
|
|
|
// 创建Cookie存储器
|
|
|
|
|
CookieStore cookieStore=new BasicCookieStore();
|
|
|
|
|
HttpClientContext httpClientContext= HttpClientContext.create();
|
|
|
|
|
httpClientContext.setCookieStore(cookieStore);
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
List<Cookie> cookies = cookieStore.getCookies();
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
if (cookie.getName().contains("GTL")) {
|
|
|
|
@ -291,37 +297,6 @@ public class GTaskClient {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
input.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
@ -331,12 +306,13 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
|
try {
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<>();
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, Charset.forName("utf8"));
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
|
// String jsString = mHttpClient.execute(httpPost,new BasicHttpClientResponseHandler());
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
@ -517,10 +493,9 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// get the task list
|
|
|
|
|
// String resString = mHttpClient.execute(httpGet,new BasicHttpClientResponseHandler());
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|