Skip to content

Commit

Permalink
use okhttp for updatechecker
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueTree242 committed Dec 24, 2021
1 parent 8710145 commit 63267b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
4 changes: 4 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand All @@ -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")
}


Expand Down
21 changes: 13 additions & 8 deletions core/src/main/java/tk/bluetree242/advancedplhide/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,16 +95,18 @@ public String getBuildDate() {

public UpdateCheckResult updateCheck() {
try {
OkHttpClient client = new OkHttpClient();
Map<String, String> 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;
}
Expand Down

0 comments on commit 63267b5

Please sign in to comment.