Giter Site home page Giter Site logo

retrofitutils's Introduction

RetrofitUtils

allprojects {

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

dependencies {

        implementation 'com.github.liangwanbo:RetrofitUtils:2.0.0'
	
}




//在application中
 OkHttpClient okHttpClient = new OkHttpConfig
 
            .Builder(this)
	
            //全局的请求头信息
	
            .setHeaders(new BuildHeadersListener() {
                @Override
                public Map<String, String> buildHeaders() {
                    HashMap<String, String> hashMap = new HashMap<>();
                    hashMap.put("appVersion", BuildConfig.VERSION_NAME);
                    hashMap.put("client", "android");
                    hashMap.put("Authorization","");
                    return hashMap;
                }
            })
	
            //开启缓存策略(默认false)
            //1、在有网络的时候,先去读缓存,缓存时间到了,再去访问网络获取数据;
            //2、在没有网络的时候,去读缓存中的数据。
            .setCache(false)
	
            .setHasNetCacheTime(10)//默认有网络时候缓存60秒
	
            .setNoNetCacheTime(3600)//默认有网络时候缓存3600秒
	
            //全局持久话cookie,保存到内存(new MemoryCookieStore())或者保存到本地(new SPCookieStore(this))
            //不设置的话,默认不对cookie做处理
            //.setCookieType(new SPCookieStore(this))
            //可以添加自己的拦截器(比如使用自己熟悉三方的缓存库等等)
	
            //.setAddInterceptor(null)
	
            //全局ssl证书认证
            //1、信任所有证书,不安全有风险(默认信任所有证书)
            //.setSslSocketFactory()
            //2、使用预埋证书,校验服务端证书(自签名证书)
            //.setSslSocketFactory(cerInputStream)
            //3、使用bks证书和密码管理客户端证书(双向认证),使用预埋证书,校验服务端证书(自签名证书)
            //.setSslSocketFactory(bksInputStream,"123456",cerInputStream)
            //全局超时配置
            .setReadTimeout(10)
            //全局超时配置
            .setWriteTimeout(10)
	
            //全局超时配置
            .setConnectTimeout(5)
	
            //全局是否打开请求log日志
            .setDebug(true)
	
            .build();
	
	


    RetrofitTask.getInstance()
            .init(this)
            .config()
            .setBaseUrl("你的baseurl")
            .setOkClient(okHttpClient);



//调用,Observer可自己定义也和用ok自带的
 RetrofitTask.getInstance()
            .createApi(DataApi.class)
            .Yzm("18736033907")
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new MyObserver() {
                @Override
                protected void onError(String code, String errorMsg) {

                }

                @Override
                protected void onSuccess(ResponseBody responseBody) {

                }
            });

retrofitutils's People

Contributors

liangwanbo avatar

Stargazers

 avatar  avatar

Watchers

 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.