Giter Site home page Giter Site logo

httprequestlib's Introduction

HttpRequestLib

一个封装了OkHttp和Retrofit的简单易用的网络库

  • 链式调用方式,简单易用
  • 配合Rxjava,支持请求取消
  • 支持每个请求使用完整的URL或使用全局的baseUrl
  • 自定义OkHttp的配置
  • 可以监听请求前和后的回调
  • 可以方便地设置全局或单个header

1.引入:

    implementation 'com.devzld:HttpRequestLib:1.0.0'

2.在APP的application里初始化:

    HttpUtil.getInstance().init(this, true, new IHttpLoader() {
                @Override
                public Retrofit getRetrofit() {
                    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    
                    OkHttpClient.Builder builder = new OkHttpClient.Builder()
                            .connectTimeout(30, TimeUnit.SECONDS)
                            .readTimeout(30, TimeUnit.SECONDS)
                            .writeTimeout(30, TimeUnit.SECONDS)
                            .addInterceptor(loggingInterceptor)
                            .retryOnConnectionFailure(true);
    
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("http://gank.io")
                            .client(builder.build())
                            .addConverterFactory(ScalarsConverterFactory.create())
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .build();
                    return retrofit;
                }
            });

其中bool值是是否是调试模式

3.使用:

	new HttpRequest("/api/random/data/Android/20")
	                .params("param1", "param1")
	                .headers("header1", "header1")
	                .callback(new IHttpCallback() {
	                    @Override
	                    public void success(String url, String res) {
	                        Toast.makeText(MainActivity.this, res, Toast.LENGTH_LONG).show();
	                    }
	
	                    @Override
	                    public void error(String url, int code, String msg) {
	                        Toast.makeText(MainActivity.this, code + msg, Toast.LENGTH_LONG).show();
	                    }
	                })
	                .get(mDisposable);

httprequestlib's People

Contributors

devzld 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

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.