Coder Social home page Coder Social logo

httputil's Introduction

HttpUtil

retrofit封装库

发起请求

get请求

 new HttpBuilder("url")
                .params(map)
                .params("key","value")
                .tag(this)//需要取消请求的tag
                .success(str->{
                    //do something surccess
                })
                .error(v->{
                    //deal something error
                })
                .get();

post请求

new HttpBuilder("url")
                .params(map)
                .params("key","value")
                .tag(this)//需要取消请求的tag
                .success(new Success() {
                    @Override
                    public void Success(String model) {

                    }
                })
                .error(new Error() {
                    @Override
                    public void Error(Object... values) {

                    }
                })
                .post();

下载文件

流式下载,直接写入文件,不存到内存,避免oom

 new HttpBuilder("http://sw.bos.baidu.com/sw-search-sp/software/c07cde08ce4/Photoshop_CS6.exe")
                .path(getExternalFilesDir(null) + File.separator + "Photoshop_CS6.exe")
                .progress(p -> {
                    progress.setText(100 * p + "%");
                })
                .success(s -> {
                    //返回path
                })
                .error(t -> {
                })
                .download();

rxjava

返回的流为异步

new HttpBuilder("url")
                .params(map)
                .params("key","value")
                .tag(this)//需要取消请求的tag
           .obpost();
               //obget() get请求
                //Obdownload()下载流;

取消请求

调用时添加tag的请求 要取消下载请求也需要给请求添加对应的独立tag

   @Override
    protected void onDestroy() {
        super.onDestroy();
        HttpUtil.cancel(this);
    }

初始化配置

请求前参数统一处理,追加渠道名称用户手机号之类

   ParamsInterceptor mParamsInterceptor = new ParamsInterceptor() {
            @Override
            public Map checkParams(Map params) {
                //追加统一参数
                params.put("app_type", "android_price");
                return params;
            }
        };

请求前headers统一处理

    HeadersInterceptor mHeadersInterceptor = new HeadersInterceptor() {
               @Override
               public Map checkHeaders(Map headers) {
                   //追加统一header,例:数据缓存一天
                   headers.put("Cache-Time", "3600*24");
                   return headers;
               }
           };

应用入口进行初始化

   new HttpUtil.SingletonBuilder(getApplicationContext())
                .baseUrl("baseurl")//URL请求前缀地址。必传
//                .addServerUrl("")//备份服务器ip地址,可多次调用传递
//                .addCallFactory()//不传默认StringConverterFactory
//                .addConverterFactory()//不传默认RxJavaCallAdapterFactory
//                .client()//OkHttpClient,不传默认OkHttp3
                .paramsInterceptor(mParamsInterceptor)//不传不进行参数统一处理
//               .headersInterceptor(mHeadersInterceptor)//不传不进行headers统一处理
                .build();

依赖添加

maven

<dependency>
  <groupId>com.gengqiquan</groupId>
  <artifactId>httputil</artifactId>
  <version>1.0.5</version>
  <type>pom</type>
</dependency>

gralde

compile 'com.gengqiquan:httputil:1.0.5'

lvy

<dependency org='com.gengqiquan' name='httputil' rev='1.0.5'>
  <artifact name='httputil' ext='pom' ></artifact>
</dependency>

httputil's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.