Skip to content

Latest commit

 

History

History
132 lines (111 loc) · 4.18 KB

README.md

File metadata and controls

132 lines (111 loc) · 4.18 KB

RequestChain

License Apache2.0

RequestChain是一个基于 EventChain + Retrofit + OkHttp 实现的网络请求框架,只需要简单的配置即可快速搭建请求环境

Download

repositories {
    maven { url 'https://www.jitpack.io' }
}

dependencies {
    implementation 'com.github.ftmtshuashua.RequestChain:library:2.0.6'
    annotationProcessor 'com.github.ftmtshuashua.RequestChain:compiler:2.0.6'
}

Use

RequestChain.setDebug(true);    // 启用日志
RequestChain.setLogTag("API");  // 日志的TAG
RequestChain.setLogBody(true);  // 显示Body日志,默认 true
RequestChain.setLogHeader(true);// 显示Header日志,默认 false
RequestChain.setFormatBody(true);       // 启用格式化Body,默认 true
RequestChain.setFormatBodyMaxLine(30);  // 格式化Body的最大显示行数,默认30行
// 定义 API
import com.acap.rc.adapter.Request;
import com.acap.rc.annotation.ApiUrl;
import com.acap.rc.annotation.ApiVariableUrl;
import com.acap.rc.annotation.ApiOkHttpConfig;
import com.acap.rc.annotation.ApiRetrofitConfig;
import okhttp3.ResponseBody;
import retrofit2.http.GET;
import retrofit2.Call;

@ApiOkHttpConfig(MyOkHttpConfig.class)      // 可选
@ApiRetrofitConfig(MyRetrofitConfig.class)  // 可选
@ApiUrl("https://getman.cn/") or @ApiVariableUrl(MyVariableUrl.class)
public interface MyApi {// Build 之后自动生成 MyApiService

    /* 使用 Request */
    @GET("mock/route/to/demo")
    Request<BaseModel<String>> getResponseBody(@Query("arg") String str);
    @GET("mock/route/to/demo")
    Request<ResponseBody> getModel();
    
    /* 使用 Retrofit 原生结果 */
    @GET("mock/route/to/demo")
    Call<ResponseBody> getCall(@Query("arg") String str);
}
// 使用 API ( EventChain 模式)
MyApiService.getResponseBody("xx")
                .chain(MyApiService.getModel())
                .listener(new OnEventDialog(this))
                .start();

// 使用 API ( retrofit2 模式)
Response<ResponseBody> execute = MyApiService.getCall("s").execute();

返回数据的 Model 中可实现 ApiBody 的方法,实现请求异常的内部扭转。可以通过 OnEventErrorListener 监听失败信息

public class BaseModel<T> implements ApiBody {
    public int code;
    public T result;
    public String msg;

    @Override
    public boolean isSuccessful() {
        return code == 0;
    }

    @Override
    public Exception getError() {
        return new Exception(msg);
    }
}

// 输出日志
4190-4546/com.acap.rc I/API: --> GET Start https://getman.cn/mock/acap/api/helloworld
4190-4546/com.acap.rc I/API:  
    ╔═══════════════════════════════════════════════════
    ║ GET  http/1.1  https://getman.cn/mock/acap/api/helloworld
    ║ END OF  --->>> GET Request
    ║ 
    ║ Response Code=200  耗时(363ms)
    ║ server: nginx
    ║ date: Fri, 19 Nov 2021 11:01:23 GMT
    ║ content-type: text/html; charset=UTF-8
    ║ x-nws-uuid-verify: ab45a34d262924443991888575e68e08
    ║ vary: Accept-Encoding
    ║ access-control-allow-origin: *
    ║ x-daa-tunnel: hop_count=1
    ║ x-cache-lookup: Hit From Upstream
    ║ x-cache-lookup: Cache Miss
    ║ last-modified: Fri, 19 Nov 2021 11:00:00 GMT
    ║ cache-control: private, no-cache
    ║ x-nws-log-uuid: 2012738967934042215
    ║ x-cache-lookup: Hit From Inner Cluster
    ║ {
    ║     "code": 0,
    ║     "result": "HelloWorld",
    ║     "msg": ""
    ║ }
    ╚═══════════════════════════════════════════════════

相关资料

OkHttp

Retrofit

EventChain