8000 GitHub - hyty/TBRetrofit: 基于square 出的 Retrofit 底层框架封装的 http请求库 · GitHub
[go: up one dir, main page]

Skip to content
forked from WCocoa/TBRetrofit

About

基于square 出的 Retrofit 底层框架封装的 http请求库

Resources

Stars

0 stars

Watchers

1 watching

Forks

 
 

Repository files navigation

pppm.jpg

TBRetrofit 1.4

Top:基于square 的 Retrofit 底层框架封装的 http库

V 1.4 更新:

1. 支持添加多个拦截器

2. 支持添加多个转换器

3. 简化Service拓展

*** V 1.0 :

1. 对各个初始化Factory进行封装,统一配置,更换日志打印拦截器,提供debug模式

2. 对Retrofit 接口定义进行了常用模式归纳,具体请求模式限定和抽象

3. 对请求参数统一格式化(GET , JSON,两种 FormData),保持入口参数设置方式相同,方便使用

4. 对CallBack返回统一使用String泛型,替换GsonConveter 为StringConverter

5. 除了CallBack 固定其他Factory类都可以解耦单独立使用

***

Gradle

Step1. 在你的根build.gradle文件中增加JitPack仓库依赖。

         allprojects {
                repositories {
                 jcenter()
                 maven { url "https://jitpack.io" }
                }
            }

Step2. 在你的module的build.gradle文件中增加TBRetrofit依赖。

        dependencies {
	          compile 'com.github.HarkBen:TBRetrofit:1.4'
	    }   

代码示例

1.初始化

            OkHttpClient okHttpClient =
                          TBOkHttpClientFactory.Builder.create()
                          .setDebug(true)
                          .syncCookie(this)
                          .setTimeout_connection(10)
                          .setTimeout_read(10)
                          .setTimeout_write(10)
                          .build();
                  TBRetrofitFactory.Builder.create()
                          .setBaseUrl(API.BASEURL)
                          .setOkHttpClient(okHttpClient)
                          .builder();
                  

初始化需要使用三个工厂来构建,这样的好处是解除隐藏关联 坏处是,构建的代码多一点,但是自己写个工具类对初始化构建进行封装就好。

2.发送请求

TBRequest 对接口操纵的简化只是提供一个思路,你完全可以按自己的方式来包装。

1.GET--NORMAL

        void GETByNormal() {
                TBRequest.create()
                        .put
8650
("user", "HarkBen")
                        .get(API.GITHUB_NORMAL, new TBCallBack() {
                            @Override
                            public void onSuccess(int code, String body) {
                                showResult(code + "--" + body);
                            }
        
                            @Override
                            public void onFailed(String errorMsg) {
                                showResult(errorMsg);
        
                            }
        
                        });
            }

 

2.GET--RESTFUL

        void GETByRestful() {
                TBRequest.create()
                        .get(API.GITHUB_RESTFUL, new String[]{"Harkben"}, new TBCallBack() {
                            @Override
                            public void onSuccess(int code, String body) {
                                showResult(code + "--" + body);
                            }
        
                            @Override
                            public void onFailed(String errorMsg) {
                                showResult(errorMsg);
                            }
                        });
            }

3.POST--Json

         void POSTByJson() {
                String url = API.LOGINTOBR;
                TBRequest.create()
                        .put("username", "tigger")
                        .put("password", "111111")
                        .put("weight", "130Kg")
                        .postJson(url, new TBCallBack() {
                            @Override
                            public void onSuccess(int code, String body) {
                                showResult(code + "--" + body);
                            }
        
                            @Override
                            public void onFailed(String errorMsg) {
                                showResult("error==" + errorMsg);
                            }
                        });
            }

4.POST--FormData

         void POSTFormData(){
                String url = API.FORMDATA;
                TBRequest.create()
                        .put("username", "cat")
                        .put("password", "222222")
                        .put("weight", "4Kg")
                        .postFormData(url, new TBCallBack() {
                            @Override
                            public void onSuccess(int code, String body) {
                                showResult(code + "--" + body);
                            }
        
                            @Override
                            public void onFailed(String errorMsg) {
                                showResult(""+errorMsg);
                            }
                        });
            }

5.POST--Multipart

         void uploadFiles(){
                File file1 = new File(parentPath+"291739323217867.png");
                File file2 = new File(parentPath+"291733413425432.png");
                File file3 = new File(parentPath+"222004413632569.png");
                List<File> files = new ArrayList<>();
                files.add(file1);
                files.add(file2);
                files.add(file3);
                TBRequest.create()
                        .put("name","小二郎他爸")
                        .put("arg",54)
                        .put("gender","男")
                        .postFormDataFiles(API.UPLOADFILE, files, new TBCallBack() {
                            @Override
                            public void onSuccess(int code, String body) {
                                showResult(code+"--"+body);
                            }
        
                            @Override
                            public void onFailed(String errorMsg) {
                                showResult("onFailed--"+errorMsg);
                            }
                        });
            }

PS:除了GET的REST 模式 在其他的请求设置参数时都可以使用 TBRequest.create().setParams(map)



** 最后附上后台接口示例代码 **:后台测试接口示例;

About

基于square 出的 Retrofit 底层框架封装的 http请求库

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages

0