Skip to content

Commit f4b5a2c

Browse files
committed
fix encoding issue
1 parent 606ae46 commit f4b5a2c

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ architectury {
1010

1111
allprojects {
1212
group = rootProject.maven_group
13-
version = rootProject.mod_version
13+
version = "$rootProject.mod_version+mc$project.minecraft_version"
1414
}
1515

1616
subprojects {

common/src/main/java/org/thinkingstudio/randomtitlerework/hitokoto/HitokotoClient.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import com.google.gson.Gson;
44
import com.mojang.authlib.minecraft.client.ObjectMapper;
5+
import org.apache.http.client.methods.HttpGet;
6+
import org.apache.http.impl.client.CloseableHttpClient;
7+
import org.apache.http.impl.client.HttpClients;
8+
import org.apache.http.util.EntityUtils;
59
import org.thinkingstudio.randomtitlerework.RandomTitleReworkMod;
610

7-
import java.io.BufferedReader;
811
import java.io.IOException;
9-
import java.io.InputStreamReader;
10-
import java.net.HttpURLConnection;
11-
import java.net.URL;
1212

1313
public class HitokotoClient {
14-
private static final String API_URL = "https://v1.hitokoto.cn";
14+
private static final String API_URL = "https://v1.hitokoto.cn/";
1515
private final ObjectMapper objectMapper;
1616

1717
public HitokotoClient() {
@@ -24,23 +24,10 @@ public HitokotoClient() {
2424
* @throws IOException 网络或解析异常
2525
*/
2626
public Hitokoto getRandomHitokoto() throws IOException {
27-
URL url = new URL(API_URL);
28-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
29-
connection.setRequestMethod("GET");
30-
connection.setRequestProperty("User-Agent", "Java Hitokoto Client");
31-
connection.setConnectTimeout(5000);
32-
connection.setReadTimeout(5000);
33-
34-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
35-
StringBuilder response = new StringBuilder();
36-
String line;
37-
while ((line = reader.readLine()) != null) {
38-
response.append(line);
39-
}
27+
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
28+
String response = EntityUtils.toString(httpClient.execute(new HttpGet(API_URL)).getEntity());
4029
RandomTitleReworkMod.LOGGER.info("Hitokoto Response String: " + response);
41-
return objectMapper.readValue(response.toString(), Hitokoto.class);
42-
} finally {
43-
connection.disconnect();
30+
return objectMapper.readValue(response, Hitokoto.class);
4431
}
4532
}
4633
}

0 commit comments

Comments
 (0)