1.首先导依赖
根build:
ext {
// Sdk and tools
minSdkVersion = 19
targetSdkVersion = 26
compileSdkVersion = 26
buildToolsVersion = '26.0.2'
//support版本
supportVersion = '27.1.0'
constraintLayout = '1.0.2'
junit = '4.12'
testRunner = '1.0.1'
espressoCore = '3.0.1'
interceptor = "3.11.0"
retrofit = '2.3.0'
converterGson = '2.3.0'
rxandroid = '1.2.1'
rxjava = '1.3.0'
rxbus = '1.0.6'
adapterRxjava = '2.0.2'
}
项目的build:
implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.interceptor"
implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofit"
implementation "com.squareup.retrofit2:converter-gson:$rootProject.converterGson"
implementation "io.reactivex:rxandroid:$rootProject.rxandroid"
implementation "io.reactivex:rxjava:$rootProject.rxjava"
implementation "com.hwangjr.rxbus:rxbus:$rootProject.rxbus"
implementation "com.squareup.retrofit2:adapter-rxjava:$rootProject.adapterRxjava"
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
2.创建一个接口能用注解去请求网络APIService
3.创建一个类Contasts,用来写接口地址
4.创建类RetrofitUtils封装
public class RetrofitUtils {
private MyApiService myApiService;
private RetrofitUtils() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(20, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.retryOnConnectionFailure(true)
.build();
//初始化Retrofit 并结合各种操作
Retrofit retrofit = new Retrofit.Builder()
//结合Gson解析
.addConverterFactory(GsonConverterFactory.create())
//结合Rxjava
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(Contacts.BASE_URL)
.client(okHttpClient)
.build();
//通过Retrofit创建完 这个ApiService 就可以调用方法了
myApiService = retrofit.create(MyApiService.class);
}
public static RetrofitUtils getInstance() {
return RetroHolder.retro;
}
private static class RetroHolder {
private static final RetrofitUtils retro = new RetrofitUtils();
}
//封装get
public RetrofitUtils get(String url, Map<String, String> map) {
//Schedulers 线程调度器
myApiService.get(url, map).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
return RetrofitUtils.getInstance();
}
//圈子列表
public RetrofitUtils get2(String url, Map<String, String> map, Map<String, String> map2) {
if (map == null) {
map = new HashMap<>();
}
if (map2 == null) {
map2 = new HashMap<>();
}
//Schedulers 线程调度器
myApiService.get2(url, map,map2).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
return RetrofitUtils.getInstance();
}
//put
public RetrofitUtils put(String url, Map<String, String> map, Map<String, String> map2) {
if (map == null) {
map = new HashMap<>();
}
if (map2 == null) {
map2 = new HashMap<>();
}
myApiService.put(url, map,map2)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
return RetrofitUtils.getInstance();
}
//重写一个观察者对象
private Observer observer = new Observer<ResponseBody>() {
@Override
public void onCompleted() {
}
//网络处理失败
@Override
public void onError(Throwable e) {
if (httpListener != null) {
Log.e("123213", "onError: ");
httpListener.onError(e.getMessage());
}
}
//网络处理成功
@Override
public void onNext(ResponseBody responseBody) {
if (httpListener != null) {
try {
httpListener.onSuccess(responseBody.string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
public interface HttpListener {
void onSuccess(String jsonStr);
void onError(String error);
}
private HttpListener httpListener;
public void setHttpListener(HttpListener listener) {
this.httpListener = listener;
}
}