When a synchronization request is made, the response value is a <T>
that is not wrapped by the Call<T>
Class of Retrofit,
and this <T>
has the same structure as the returned value of the called API
Maven:
<dependency>
<groupId>io.github.easyretrofit</groupId>
<artifactId>adapter-simple-body</artifactId>
<version>${latest.version}</version> <!-- 替换为实际的版本号 -->
</dependency>
Gradle:
implementation 'io.github.easyretrofit:adapter-simple-body:${latest.version}'
public class SimpleBodyCallAdapterFactoryBuilder extends BaseCallAdapterFactoryBuilder {
@Override
public Converter.Factory build() {
return SimpleBodyCallAdapterFactory.create();
}
}
@RetrofitBuilder(baseUrl = "${app.backend.url}",
addConverterFactory = {GsonConvertFactoryBuilder.class},
addCallAdapterFactory = {SimpleBodyCallAdapterFactoryBuilder.class})
public interface HelloApi {
}
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(SimpleBodyCallAdapterFactory.create())
.build();
if you add any other call adapter, and those call adapter maybe has conflict with the current call adapter, you can set the call adapter type to exclude
by default, the current call adapter already exclude the official call adapter type (Call, Flowable, Observable, Single, etc.), so current call adapter will not handle the official call adapter type. so, you can use this call adapter with official call adapter.
this library provide two methods to set the call adapter type to exclude.
public static SimpleBodyCallAdapterFactory create(Class<?>[] exclude) {
return new SimpleBodyCallAdapterFactory(exclude, null);
}
public static SimpleBodyCallAdapterFactory create(Class<?>[] exclude, Function<ErrorParameter, ?> customErrorFunction) {
return new SimpleBodyCallAdapterFactory(exclude, customErrorFunction);
}
If your response returns an error Body and the body cannot be parsed by the converter(json, xml or others), resulting in an exception being returned,you can set global exception handling or customize the handling return value
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(SimpleBodyCallAdapterFactory.create(errorParameter -> {
throw new RuntimeException(errorParameter.getResponse().toString());
}))
.build();
If your response returns an error Body and the body cannot be parsed by the converter(json, xml or others), resulting in an exception being returned, you can use this annotation to map the values in your response. If your response object contains fields such as http status code, error message, etc
public class Result<T> {
private int code;
private T data;
private String msg;
public Result(){
}
public Result(int code, T data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
public interface MyServiceApi {
@GET("/hello")
@ErrorResponseBody(codeFieldName = "code", codeType = int.class, messageFieldName = "msg", messageType = String.class)
Result<List<HelloBean>> getHellos();
}
if you not use easy-retrofit-adapter-simple-body
, you should use retrofit2 Call<T>
to get the response value
myServiceApi interface:
public interface MyServiceApi {
@GET("/hello")
Call<Result<List<HelloBean>>> getCallHellos();
}
Api usage:
MyServiceApi myServiceApi = retrofit.create(MyServiceApi.class);
Call<Result<List<HelloBean>>> callHellos = myServiceApi.getCallHellos();
Result<List<HelloBean>> body = callHellos.execute().body();
if you use easy-retrofit-adapter-simple-body
, you can use the Result<T>
to get the response value
myServiceApi interface:
public interface MyServiceApi {
@GET("/hello")
Result<List<HelloBean>> getHellos();
}
Api usage:
Result<List<HelloBean>> hellos = myServiceApi.getHellos();
the example code you can see current project UT