1.保证建造的类单例
private RetrofitManager(){}; private static RetrofitManager retrofitManager=new RetrofitManager(); public static RetrofitManager getInstance(){ return retrofitManager; }
2.导入依赖
//RxJava依赖
implementation 'io.reactivex.rxjava2:rxjava:2.2.6'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
//Retrofit依赖
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
//Gson converter gson解析
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
//RxJava2 Adapter
implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
//okhttp
implementation 'com.squareup.okhttp3:okhttp:3.4.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
3.retrofit属性
private Retrofit retrofit;
4.创建retrofit对象
5.重写get方法 并调用
public Retrofit getRetrofit() { if (retrofit==null){ create(); } return retrofit; }