From 63267b59c6ca5b464ca37f1f1cf5b29dbbeee9b9 Mon Sep 17 00:00:00 2001 From: BlueTree242 Date: Fri, 24 Dec 2021 15:21:32 +0200 Subject: [PATCH] use okhttp for updatechecker --- core/build.gradle | 4 ++++ .../bluetree242/advancedplhide/Platform.java | 21 ++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/core/build.gradle b/core/build.gradle index 0120c91..a41a015 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -34,6 +34,8 @@ rootProject.allprojects { relocate 'space.arim.dazzleconf', "tk.bluetree242.advancedplhide.dependencies.dazzleconf" relocate 'org.yaml.snakeyaml', "tk.bluetree242.advancedplhide.dependencies.yaml" relocate 'org.json', "tk.bluetree242.advancedplhide.dependencies.json" + relocate 'okio', "tk.bluetree242.advancedplhide.dependencies.okio" + relocate 'okhttp3', "tk.bluetree242.advancedplhide.dependencies.okhttp" } } @@ -50,6 +52,8 @@ dependencies { implementation 'space.arim.dazzleconf:dazzleconf-ext-snakeyaml:1.2.0-M2' compileOnly 'com.mojang:brigadier:1.0.18' implementation group: 'org.json', name: 'json', version: '20210307' + implementation(platform("com.squareup.okhttp3:okhttp-bom:4.9.0")) + implementation("com.squareup.okhttp3:okhttp") } diff --git a/core/src/main/java/tk/bluetree242/advancedplhide/Platform.java b/core/src/main/java/tk/bluetree242/advancedplhide/Platform.java index 9ac4372..ad14103 100644 --- a/core/src/main/java/tk/bluetree242/advancedplhide/Platform.java +++ b/core/src/main/java/tk/bluetree242/advancedplhide/Platform.java @@ -22,11 +22,14 @@ package tk.bluetree242.advancedplhide; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; import org.json.JSONObject; import tk.bluetree242.advancedplhide.config.Config; import tk.bluetree242.advancedplhide.exceptions.ConfigurationLoadException; import tk.bluetree242.advancedplhide.impl.version.UpdateCheckResult; -import tk.bluetree242.advancedplhide.utils.HttpPostMultipart; import java.util.ArrayList; import java.util.HashMap; @@ -92,16 +95,18 @@ public String getBuildDate() { public UpdateCheckResult updateCheck() { try { + OkHttpClient client = new OkHttpClient(); Map headers = new HashMap<>(); headers.put("User-Agent", "APH/Java"); - HttpPostMultipart req = new HttpPostMultipart("https://advancedplhide.ml/updatecheck", "utf-8", headers); - req.addFormField("version", getCurrentVersion()); - req.addFormField("buildNumber", getCurrentBuild()); - req.addFormField("buildDate", getBuildDate()); - req.addFormField("devUpdatechecker", getConfig().dev_updatechecker() + ""); - String response = req.finish(); + MultipartBody form = new MultipartBody.Builder().setType(MediaType.get("multipart/form-data")) + .addFormDataPart("version", getCurrentVersion()) + .addFormDataPart("buildNumber", getCurrentBuild()) + .addFormDataPart("buildDate", getCurrentBuild()) + .addFormDataPart("devUpdatechecker", getConfig().dev_updatechecker() + "").build(); + Request req = new Request.Builder().url("https://advancedplhide.ml/updatecheck").post(form).build(); + String response = client.newCall(req).execute().body().string(); JSONObject json = new JSONObject(response); - return new UpdateCheckResult(json.getInt("versions_behind"), json.isNull("versions_behind") ? null : json.getString("message"), json.isNull("type") ? "INFO" : json.getString("type"), json.getString("downloadUrl")); + return new UpdateCheckResult(json.getInt("versions_behind"), json.isNull("versions_behind") ? null : json.isNull("message") ? null : json.getString("message"), json.isNull("type") ? "INFO" : json.getString("type"), json.getString("downloadUrl")); } catch (Exception e) { return null; }