From 91ef5e3bc3249a3d04a820a8ac0df82b144b5743 Mon Sep 17 00:00:00 2001 From: Timi Date: Sat, 19 Jul 2025 17:27:29 +0800 Subject: [PATCH] fix GsonRequest serialize fail --- .../java/com/imyeyu/network/GsonRequest.java | 24 ++++++++++------- .../java/com/imyeyu/network/TimiRequest.java | 26 ++++++++++++------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/imyeyu/network/GsonRequest.java b/src/main/java/com/imyeyu/network/GsonRequest.java index cd19417..5d7a4d9 100644 --- a/src/main/java/com/imyeyu/network/GsonRequest.java +++ b/src/main/java/com/imyeyu/network/GsonRequest.java @@ -14,7 +14,7 @@ import java.io.IOException; * @author 夜雨 * @since 2025-07-15 14:27 */ -public class GsonRequest { +public class GsonRequest { private static final Gson GSON = new Gson(); @@ -30,25 +30,29 @@ public class GsonRequest { return TimiJava.firstNotNull(gson, GSON); } - public static GsonRequest wrap(Request request) { - return new GsonRequest<>(request); + public static GsonRequest wrap(Request request) { + return new GsonRequest(request); } - public static GsonRequest get(String url) { - return new GsonRequest<>(Request.get(url)); + public static GsonRequest get(String url) { + return new GsonRequest(Request.get(url)); } - public static GsonRequest post(String url) { - return new GsonRequest<>(Request.post(url)); + public static GsonRequest post(String url) { + return new GsonRequest(Request.post(url)); } - public GsonRequest gson(Gson gson) { + public GsonRequest gson(Gson gson) { this.gson = gson; return this; } - public T result() throws IOException { - return getGson().fromJson(request.execute().returnContent().asString(), new TypeToken() {}.getType()); + public T resultAs(Class clazz) throws IOException { + return getGson().fromJson(request.execute().returnContent().asString(), clazz); + } + + public T resultAs(TypeToken typeToken) throws IOException { + return getGson().fromJson(request.execute().returnContent().asString(), typeToken); } public JsonObject asJsonObject() throws IOException { diff --git a/src/main/java/com/imyeyu/network/TimiRequest.java b/src/main/java/com/imyeyu/network/TimiRequest.java index 5d058f3..bb18e7c 100644 --- a/src/main/java/com/imyeyu/network/TimiRequest.java +++ b/src/main/java/com/imyeyu/network/TimiRequest.java @@ -10,26 +10,34 @@ import java.io.IOException; * @author 夜雨 * @since 2025-07-15 14:34 */ -public class TimiRequest extends GsonRequest { +public class TimiRequest extends GsonRequest { protected TimiRequest(Request request) { super(request); } - public static TimiRequest wrap(Request request) { - return new TimiRequest<>(request); + public static TimiRequest wrap(Request request) { + return new TimiRequest(request); } - public static TimiRequest get(String url) { - return new TimiRequest<>(Request.get(url)); + public static TimiRequest get(String url) { + return new TimiRequest(Request.get(url)); } - public static TimiRequest post(String url) { - return new TimiRequest<>(Request.post(url)); + public static TimiRequest post(String url) { + return new TimiRequest(Request.post(url)); } - public T result() throws IOException { - TimiResponse resp = getGson().fromJson(request.execute().returnContent().asString(), new TypeToken>() {}.getType()); + public T resultAs(Class clazz) throws IOException { + TimiResponse resp = getGson().fromJson(request.execute().returnContent().asString(), TypeToken.getParameterized(TimiResponse.class, clazz).getType()); + if (resp.isFail()) { + throw resp.toException(); + } + return resp.getData(); + } + + public T resultAs(TypeToken typeToken) throws IOException { + TimiResponse resp = getGson().fromJson(request.execute().returnContent().asString(), TypeToken.getParameterized(TimiResponse.class, typeToken.getType()).getType()); if (resp.isFail()) { throw resp.toException(); }