Other: 中文版
dependencies {
api 'com.tpa.client:tina:1.1.2'
annotationProcessor 'com.tpa.client:tina-compiler:1.0.0'
}
## request
-keep public class * extends com.tpa.client.tina.model.TinaBaseRequest {
public void set*(***);
public *** get*();
public *** is*();
}
## response
-keep public class * extends ${BaseResponseClass} {
*;
}
public interface TinaConfig {
/** okhttpclient config **/
public @NonNull OkHttpClient getOkhttpClient();
/** mediaType config **/
public @NonNull MediaType getMediaType();
/** host url **/
public @NonNull String getHost();
public @Nonable TinaFilter getTinaFilter();
/** It is typically used to encrypt the request body data **/
public @Nullable TinaConvert getRequestConvert();
}
Tina.initConfig(tinaConfig);
@Post("url")
public class Reqest extends TinaBaseRequest{
private String name = "tqf";
private String sex = "man";
}
------------------------------------------------------
Reqest request = new Reqest();
Tina.build()
.call(request)
.callBack(new TinaSingleCallBack<Response>() {
@Override
public void onSuccess(Response response) {
}
@Override
public void onFail(TinaException e) {
}
})
.request();
@Post("url")
public class Reqest extends TinaBaseRequest{
private String name = "tqf";
private String sex = "man";
}
------------------------------------------
Reqest request = new Reqest();
Tina.build(Tina.CHAINS)
.call(request)
.call(request)
.callBack(new TinaChainCallBack<TinaBaseResponse>() {
@Override
public Object onSuccess(Object feedbackResult,TinaBaseResponse response) {
return null;
}
@Override
public void onFail(TinaException e) {
}
})
.callBack(new TinaChainCallBack<TinaBaseResponse>() {
@Override
public Object onSuccess(Object feedbackResult, TinaBaseResponse response) {
return null;
}
@Override
public void onFail(TinaException e) {
}
})
.request();
- feedbackResult : The result passed from the previous request
- return : The result passed to the next request. If TinaChain.FUSING is returned, the chain request is interrupted.
@Post("url")
public class Reqest extends TinaBaseRequest{
private String name = "tqf";
private String sex = "man";
}
----------------------------------------------------------
Reqest request = new Reqest();
Tina.build(Tina.CONCURRENT)
.call(request)
.call(request)
.callBack(new TinaSingleCallBack<TinaBaseResponse>() {
@Override
public void onSuccess(TinaBaseResponse response) {
}
@Override
public void onFail(TinaException e) {
}
})
.callBack(new TinaSingleCallBack<TinaBaseResponse>() {
@Override
public void onSuccess(TinaBaseResponse response) {
}
@Override
public void onFail(TinaException e) {
}
})
.request();
...
.startCallBack(new TinaStartCallBack() {
@Override
public void start() {
}
})
.endCallBack(new TinaEndCallBack() {
@Override
public void end() {
}
})
...
Tina.build()
.filter(new TinaFilter() {
@Override
public TinaFilterResult filter(TinaBaseRequest request, byte[] body, Class expect) {
return null;
}
})
...
Tina.build()
.filter(BitmapFilter.build())
.callBack(request)
.callBack(new TinaSingleCallBack<Bitmap>() {
@Override
public void onSuccess(Bitmap bitmap) {
}
@Override
public void onFail(TinaException e) {
}
})
.request();
Tina.build()
.deamon(activity)
...
A request will be cancelled with the activity's destoryed
@AutoMode
public class AnswererListResponse {
}
The @Automodel annotated resposne recursively loops around the entire response model, filling in all the empty objects.
if(data != null && data.getData1() != null && data.getData1().getData2 != null){
do(data.getData1().getData2());
}
else{
//do somethings
}
do(data.getData1().getData2());
Ignore inject
@AutoMode
public class Response {
@NumberScale(2)
private String data = 3.1415926; // 3.14
}
@AutoMode
public class Response {
@NotNull(message = "data field cannot be empty")
private String data;
}
See the implementation of @notnull and @numberscale for details
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface NotNull {
public String message() default "";
}
public class NotNullHandler implements TinaAnnotationHandler<NotNull>{
@Override
public void hanld(NotNull annotation, Object host, Field field) throws TinaDataException{
try {
Object o = field.get(host);
if (o == null) {
throw new TinaDataException(annotation.message());
}
} catch (IllegalAccessException e) {
}
}
}
TinaAnnotationManager.getInstance().register(NotNull.class , new NotNullHandler());
@Cache(key = "key" , expire = 1000 , unit = TimeUnit.SECONDS)
public class Request extends TinaBaseRequest {
}
Request request = new Request();
// If the cache is hit, no network request is called. Single CallBack
Tina.build()
.call(request)
.callBack(new TinaSingleCallBack<Response>() {
@Override
public void onSuccess(Response response) {
//response
}
@Override
public void onFail(TinaException exception) {
}
})
.request();
// A network request is called regardless of whether the cache is hit or not. Double CallBack
Tina.build()
.call(request)
.callBack(new TinaSingleCacheCallBack<Response>() {
@Override
public void onSuccess(Response response) {
//fresh response
}
@Override
public void onCache(Response response) {
//cache response
}
@Override
public void onFail(TinaException exception) {
}
})
.request();
support
POST
、GET
、PUT
、DELETE
、PATCH
。
@Delete("/name/{name}/sex/{sex}")
public class Reqest extends TinaBaseRequest{
@Path("name")
private String name = "tqf";
@Path("sex")
private String sex = "man";
}
>>> DELETE /name/tqf/sex/man
@Delete("/name/:name/sex/:sex")
public class Reqest extends TinaBaseRequest{
private String name = "tqf";
private String sex = "man";
}
>>> DELETE /name/tqf/sex/man
@Delete("/name/:name/sex/:sex?")
public class Reqest extends TinaBaseRequest{
private String name = "tqf";
private String sex = null;
}
>>> DELETE /name/tqf/sex
@ConfigId("Pay")
class Config implements TinaConfig{
...
}
Tina.addConfig(new Config());
--- gradle clean build ---
/*
* Automatically generate PayTina
*/
PayTina.build()...
First, download the settings.jar and keep it local >>>>> settings.jar
Open AndroidStudio and select AndroidStudio - File - Import Setttings
Select the setting.jar
- tina_contract : Generate model contract
- tina_singleReq : Generate a simple request
- tina_singleReq2 : Generate simple requests with endCallBack and startCallBack
- tina_chainReq : Generate chain request
- tina_chainReq2 : Generate chain requests with endCallBack and startCallBack
- tina_concurrentReq : Generate a concurrent request
- tina_concurrentReq2 : Generate a concurrent request with endCallBack and startCallBack
Provided as a simple reference, live templates can be modified to suit your business needs