Coder Social home page Coder Social logo

jwhttpwrapper's Introduction

JWHttpWrapper 主要功能

  1. 基于OKHttp的网络库,底层依赖很小,只有一个文件依赖于OKhttp, 可以快速切换为其它网络库。
  2. 支持本地文件缓存以及多样化的缓存方式。
  3. 自定义Interceptor。可以拦截数据进行处理
  4. 请求返回数据处理在单独现成进行,提供的回调方法在UI现成进行,可以直接用返回的数据更新UI
  5. 支持发送请求的时候设置Tag, 然后单个或者批量取消请求
  6. 支持进度提示

使用说明

  • 初始化
RequestConfig config = new RequestConfig.Builder(this).logEnable(true).cacheMode(RequestConfig.HttpCacheMode.NO_CACHE)
                .baseUrl("http://ip.taobao.com/").cacheTimeInSeconds(3 * 60).connectionTimeOut(30 *1000).build();
HttpRequestAgent.getInstance().init(config);
  • 发送请求
RequestParams params = new RequestParams.Builder().requestParams(requestParams).url("service/getIpInfo.php").cacheMode(RequestConfig.HttpCacheMode.ALWAYS_CACHE).post().build();
HttpRequestAgent.getInstance().executeRequest(params, callback);
  • 缓存设置
ALWAYS_CACHE,//缓存时间内,不发请求,直接返回缓存结果
CACHE_FIRST,//优先返回缓存结果,然后发送请求.(总共返回二次数据)
FAILED_SHOW_CACHE,//请求失败后展示缓存
CACHE_WHEN_NO_NETWORK//没有网络的时候展示缓存
  • Interceptor使用
RequestConfig config = new RequestConfig.Builder(this).logEnable(true).cacheMode(RequestConfig.HttpCacheMode.NO_CACHE)
                .baseUrl("http://ip.taobao.com/").addInterceptor(new HandlerInterceptor() {
                    @Override
                    public boolean preHandler(RequestParams params) {
                        //TODO 请求之前的拦截  返回值决定是否继续请求
                        return true;
                    }

                    @Override
                    public void postSuccessHandler(RequestParams params, int statusCode, Map<String, List<String>> headers) {
                        //TODO  请求成功的拦截
                    }

                    @Override
                    public void postFailedHandler(IOException exception) {
                        //TODO 请求失败的拦截器
                    }

                    @Override
                    public void afterCompletion(RequestParams params, ResponseData<String> responseData) {
                        //TODO 请求逻辑处理完毕的回调
                    }
                }).cacheTimeInSeconds(3 * 60).connectionTimeOut(30 *1000).build());
  • 返回数据

返回数据结果为ResponseData类型, 可以对象序列化直接返回JavaBean,只需要设置Callback为ObjectCallback,例如:

 ObjectCallback<DemoResultBean<Location>> objCallback = new ObjectCallback<DemoResultBean<Location>>(new TypeReference<DemoResultBean<Location>>(){}) {
        @Override
        protected void onResponse(ResponseData<DemoResultBean<Location>> responseData) {
            if (responseData.isSuccess()){
                if (responseData.isFromCache()){
                    tvCacheResult.setText(responseData.toString());
                }else{
                    tvRequestResult.setText(responseData.toString());
                }
            }else{
                Toast.makeText(MainActivity.this, responseData.getDescription(), Toast.LENGTH_LONG).show();
            }
        }
        
        @Override
        public void onProgress(long bytesWritten, long totalSize) {
            super.onProgress(bytesWritten, totalSize);
            //TODO  you can update ui here
        }
    }

支持设置文件路径,直接下载生成为文件

fileCallback = new FileCallback(downloadFile.getAbsolutePath()) {
            @Override
            protected void onResponse(ResponseData<File> responseData) {
                File file = responseData.getData();
                if (responseData.isSuccess()){
                    if (responseData.isFromCache()){
                        tvCacheResult.setText(responseData.toString() + "\n文件最后修改时间" + new Date(file.lastModified()) + "\n文件路径:" + file.getAbsolutePath());
                    }else{
                        tvRequestResult.setText(responseData.toString() + "\n文件最后修改时间" + new Date(file.lastModified()) + "\n文件路径:" + file.getAbsolutePath());
                    }
                }else{
                    Toast.makeText(MainActivity.this, responseData.getDescription(), Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onProgress(long bytesWritten, long totalSize) {
                super.onProgress(bytesWritten, totalSize);
            }
        };
        
  • 取消请求
HttpRequestAgent.getInstance().interruptRequestByTag(tag1, tag2, tag3);//取消设置这三个tag的请求
HttpRequestAgent.getInstance().interruptAllRequest();//取消所有请求

jwhttpwrapper's People

Contributors

jclick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.