update 优化下写法
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user