From be685a374558199647411c6a0737535204a5684e Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Mon, 22 Apr 2024 13:14:11 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=E4=B8=8B=E5=86=99?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/yutou/nettools/MainActivity.java | 17 ++++++------ .../converter/JsonConverterFactory.java | 2 +- .../converter/JsonResponseBodyConverter.java | 10 +++++-- .../com/yutou/netlibs/http/HttpCallback.java | 27 +++++++++++++++++++ 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 netlibs/src/main/java/com/yutou/netlibs/http/HttpCallback.java diff --git a/app/src/main/java/com/yutou/nettools/MainActivity.java b/app/src/main/java/com/yutou/nettools/MainActivity.java index 3811900..cab33be 100644 --- a/app/src/main/java/com/yutou/nettools/MainActivity.java +++ b/app/src/main/java/com/yutou/nettools/MainActivity.java @@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import com.yutou.netlibs.http.HttpBody; +import com.yutou.netlibs.http.HttpCallback; import java.util.List; @@ -21,25 +22,25 @@ public class MainActivity extends AppCompatActivity { findViewById(R.id.button) .setOnClickListener(v -> { Api.getInstance().getList("") - .enqueue(new Callback>>() { + .enqueue(new HttpCallback>() { @Override - public void onResponse(Call>> call, Response>> response) { - System.out.println("response = " + response.body().getData()); + public void onResponse(int code, String status, List response) { + System.out.println("code = " + code + ", status = " + status + ", response = " + response); } @Override - public void onFailure(Call>> call, Throwable t) { + public void onFailure(Throwable throwable) { } }); - Api.getInstance().getVersion().enqueue(new Callback>() { + Api.getInstance().getVersion().enqueue(new HttpCallback() { @Override - public void onResponse(Call> call, Response> response) { - System.out.println("response = " + response.body().getData()); + public void onResponse(int code, String status, Bean response) { + System.out.println("code = " + code + ", status = " + status + ", response = " + response); } @Override - public void onFailure(Call> call, Throwable t) { + public void onFailure(Throwable throwable) { } }); diff --git a/netlibs/src/main/java/com/yutou/netlibs/converter/JsonConverterFactory.java b/netlibs/src/main/java/com/yutou/netlibs/converter/JsonConverterFactory.java index 668fc40..35969c4 100644 --- a/netlibs/src/main/java/com/yutou/netlibs/converter/JsonConverterFactory.java +++ b/netlibs/src/main/java/com/yutou/netlibs/converter/JsonConverterFactory.java @@ -25,6 +25,6 @@ public class JsonConverterFactory extends Converter.Factory { @Override public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { // return super.responseBodyConverter(type, annotations, retrofit); - return new JsonResponseBodyConverter<>(); + return new JsonResponseBodyConverter<>(type); } } diff --git a/netlibs/src/main/java/com/yutou/netlibs/converter/JsonResponseBodyConverter.java b/netlibs/src/main/java/com/yutou/netlibs/converter/JsonResponseBodyConverter.java index d75137f..b2eb260 100644 --- a/netlibs/src/main/java/com/yutou/netlibs/converter/JsonResponseBodyConverter.java +++ b/netlibs/src/main/java/com/yutou/netlibs/converter/JsonResponseBodyConverter.java @@ -8,16 +8,22 @@ import org.jetbrains.annotations.Nullable; import retrofit2.Converter; import java.io.IOException; +import java.lang.reflect.Type; public class JsonResponseBodyConverter implements Converter { + Type type; + public JsonResponseBodyConverter(Type type) { + this.type=type; + } + @Nullable @Override public T convert(ResponseBody responseBody) throws IOException { String string = new String(responseBody.bytes()); responseBody.close(); - HttpBody body ; + HttpBody body ; try { - body = JSONObject.parseObject(string, HttpBody.class); + body = JSONObject.parseObject(string, type); return (T) body; } catch (Exception e) { e.printStackTrace(); diff --git a/netlibs/src/main/java/com/yutou/netlibs/http/HttpCallback.java b/netlibs/src/main/java/com/yutou/netlibs/http/HttpCallback.java new file mode 100644 index 0000000..93b97a3 --- /dev/null +++ b/netlibs/src/main/java/com/yutou/netlibs/http/HttpCallback.java @@ -0,0 +1,27 @@ +package com.yutou.netlibs.http; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +public abstract class HttpCallback implements Callback> { + + public abstract void onResponse(int code,String status,T response); + public abstract void onFailure(Throwable throwable); + + @Override + public void onResponse(Call> call, Response> response) { + if (response.body() != null) { + System.out.println("response = " + response.body()); + System.out.println("response.data = " + response.body().getData()); + onResponse(response.body().getCode(),response.body().getMsg(),response.body().getData()); + }else{ + onFailure(new NullPointerException("response body is null")); + } + } + + @Override + public void onFailure(Call> call, Throwable throwable) { + onFailure(throwable); + } +}