update 优化下写法

This commit is contained in:
2024-04-22 13:14:11 +08:00
parent 0d65df5271
commit be685a3745
4 changed files with 45 additions and 11 deletions

View File

@@ -25,6 +25,6 @@ public class JsonConverterFactory extends Converter.Factory {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
// return super.responseBodyConverter(type, annotations, retrofit);
return new JsonResponseBodyConverter<>();
return new JsonResponseBodyConverter<>(type);
}
}

View File

@@ -8,16 +8,22 @@ import org.jetbrains.annotations.Nullable;
import retrofit2.Converter;
import java.io.IOException;
import java.lang.reflect.Type;
public class JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
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<T> body ;
try {
body = JSONObject.parseObject(string, HttpBody.class);
body = JSONObject.parseObject(string, type);
return (T) body;
} catch (Exception e) {
e.printStackTrace();

View File

@@ -0,0 +1,27 @@
package com.yutou.netlibs.http;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public abstract class HttpCallback<T> implements Callback<HttpBody<T>> {
public abstract void onResponse(int code,String status,T response);
public abstract void onFailure(Throwable throwable);
@Override
public void onResponse(Call<HttpBody<T>> call, Response<HttpBody<T>> 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<HttpBody<T>> call, Throwable throwable) {
onFailure(throwable);
}
}