修改了GTaskClient文件夹中,http过时导致的错误

sy
feifei 2 months ago
parent 50ef84f1f6
commit 85807262dc

@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}

@ -43,8 +43,6 @@ android {
excludes += "META-INF/ASL2.0" excludes += "META-INF/ASL2.0"
} }
} }
useLibrary("org.apache.http.legacy")
} }
dependencies { dependencies {
@ -52,9 +50,14 @@ dependencies {
implementation(libs.material) implementation(libs.material)
implementation(libs.activity) implementation(libs.activity)
implementation(libs.constraintlayout) implementation(libs.constraintlayout)
implementation("org.apache.httpcomponents:httpclient-android:4.3.5.1") {
exclude(group = "org.apache.httpcomponents", module = "httpcore") // OkHttp dependencies
implementation("com.squareup.okhttp3:okhttp:3.14.9") {
exclude(group = "org.bouncycastle", module = "bcprov-jdk15on")
exclude(group = "org.bouncycastle", module = "bcpkix-jdk15on")
} }
implementation("com.squareup.okio:okio:1.17.5")
testImplementation(libs.junit) testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit) androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core) androidTestImplementation(libs.espresso.core)

@ -32,20 +32,6 @@ import net.micode.notes.gtask.exception.NetworkFailureException;
import net.micode.notes.tool.GTaskStringUtils; import net.micode.notes.tool.GTaskStringUtils;
import net.micode.notes.ui.NotesPreferenceActivity; 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -54,12 +40,25 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.LinkedList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater; import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream; import java.util.zip.InflaterInputStream;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class GTaskClient { public class GTaskClient {
private static final String TAG = GTaskClient.class.getSimpleName(); private static final String TAG = GTaskClient.class.getSimpleName();
@ -72,7 +71,7 @@ public class GTaskClient {
private static GTaskClient mInstance = null; private static GTaskClient mInstance = null;
private DefaultHttpClient mHttpClient; private OkHttpClient mHttpClient;
private String mGetUrl; private String mGetUrl;
@ -91,7 +90,23 @@ public class GTaskClient {
private JSONArray mUpdateArray; private JSONArray mUpdateArray;
private GTaskClient() { private GTaskClient() {
mHttpClient = null; mHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.cookieJar(new CookieJar() {
private final List<Cookie> cookies = new ArrayList<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
this.cookies.addAll(cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
return cookies;
}
})
.build();
mGetUrl = GTASK_GET_URL; mGetUrl = GTASK_GET_URL;
mPostUrl = GTASK_POST_URL; mPostUrl = GTASK_POST_URL;
mClientVersion = -1; mClientVersion = -1;
@ -228,26 +243,23 @@ public class GTaskClient {
private boolean loginGtask(String authToken) { private boolean loginGtask(String authToken) {
int timeoutConnection = 10000; int timeoutConnection = 10000;
int timeoutSocket = 15000; int timeoutSocket = 15000;
HttpParams httpParameters = new BasicHttpParams(); OkHttpClient.Builder builder = new OkHttpClient.Builder();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); builder.connectTimeout(timeoutConnection, TimeUnit.MILLISECONDS);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); builder.readTimeout(timeoutSocket, TimeUnit.MILLISECONDS);
mHttpClient = new DefaultHttpClient(httpParameters); mHttpClient = builder.build();
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
mHttpClient.setCookieStore(localBasicCookieStore);
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
// login gtask // login gtask
try { try {
String loginUrl = mGetUrl + "?auth=" + authToken; Request request = new Request.Builder()
HttpGet httpGet = new HttpGet(loginUrl); .url(mGetUrl + "?auth=" + authToken)
HttpResponse response = null; .build();
response = mHttpClient.execute(httpGet); Response response = mHttpClient.newCall(request).execute();
// get the cookie now // get the cookie now
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies(); List<Cookie> cookies = mHttpClient.cookieJar().loadForRequest(request.url());
boolean hasAuthCookie = false; boolean hasAuthCookie = false;
for (Cookie cookie : cookies) { for (Cookie cookie : cookies) {
if (cookie.getName().contains("GTL")) { if (cookie.name().contains("GTL")) {
hasAuthCookie = true; hasAuthCookie = true;
} }
} }
@ -256,7 +268,7 @@ public class GTaskClient {
} }
// get the client version // get the client version
String resString = getResponseContent(response.getEntity()); String resString = getResponseContent(response.body().byteStream());
String jsBegin = "_setup("; String jsBegin = "_setup(";
String jsEnd = ")}</script>"; String jsEnd = ")}</script>";
int begin = resString.indexOf(jsBegin); int begin = resString.indexOf(jsBegin);
@ -284,26 +296,27 @@ public class GTaskClient {
return mActionId++; return mActionId++;
} }
private HttpPost createHttpPost() { private Request createRequest() {
HttpPost httpPost = new HttpPost(mPostUrl); Request.Builder builder = new Request.Builder()
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); .url(mPostUrl)
httpPost.setHeader("AT", "1"); .addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
return httpPost; .addHeader("AT", "1");
return builder.build();
} }
private String getResponseContent(HttpEntity entity) throws IOException { private String getResponseContent(InputStream inputStream) throws IOException {
String contentEncoding = null; String contentEncoding = null;
if (entity.getContentEncoding() != null) { if (inputStream != null) {
contentEncoding = entity.getContentEncoding().getValue(); contentEncoding = inputStream.toString();
Log.d(TAG, "encoding: " + contentEncoding); Log.d(TAG, "encoding: " + contentEncoding);
} }
InputStream input = entity.getContent(); InputStream input = inputStream;
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) { if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
input = new GZIPInputStream(entity.getContent()); input = new GZIPInputStream(inputStream);
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) { } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
Inflater inflater = new Inflater(true); Inflater inflater = new Inflater(true);
input = new InflaterInputStream(entity.getContent(), inflater); input = new InflaterInputStream(inputStream, inflater);
} }
try { try {
@ -329,22 +342,18 @@ public class GTaskClient {
throw new ActionFailureException("not logged in"); throw new ActionFailureException("not logged in");
} }
HttpPost httpPost = createHttpPost(); Request request = createRequest();
RequestBody body = new FormBody.Builder()
.add("r", js.toString())
.build();
Request postRequest = request.newBuilder()
.post(body)
.build();
try { try {
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>(); Response response = mHttpClient.newCall(postRequest).execute();
list.add(new BasicNameValuePair("r", js.toString())); String jsString = getResponseContent(response.body().byteStream());
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); return new JSONObject(jsString);
} catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("postRequest failed");
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
e.printStackTrace(); e.printStackTrace();
@ -516,12 +525,13 @@ public class GTaskClient {
} }
try { try {
HttpGet httpGet = new HttpGet(mGetUrl); Request request = new Request.Builder()
HttpResponse response = null; .url(mGetUrl)
response = mHttpClient.execute(httpGet); .build();
Response response = mHttpClient.newCall(request).execute();
// get the task list // get the task list
String resString = getResponseContent(response.getEntity()); String resString = getResponseContent(response.body().byteStream());
String jsBegin = "_setup("; String jsBegin = "_setup(";
String jsEnd = ")}</script>"; String jsEnd = ")}</script>";
int begin = resString.indexOf(jsBegin); int begin = resString.indexOf(jsBegin);
@ -532,10 +542,6 @@ public class GTaskClient {
} }
JSONObject js = new JSONObject(jsString); JSONObject js = new JSONObject(jsString);
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS); 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) { } catch (IOException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
e.printStackTrace(); e.printStackTrace();

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
Loading…
Cancel
Save