package com.dejun.commonsdk.net; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; /** * Author:DoctorWei * Time:2018/12/4 16:37 * Description: * email:1348172474@qq.com */ public class RetrofitClient { private static RetrofitClient retrofitClient; private RetrofitClient() { } /** * 懒汉多线程单例 * * @return */ public static RetrofitClient getInstance() { if (retrofitClient == null) { synchronized (RetrofitClient.class) { if (retrofitClient == null) { retrofitClient = new RetrofitClient(); } } } return retrofitClient; } public Retrofit getRetrofit(String baseUrl){ Retrofit retrofit=new Retrofit.Builder() .client(setOkHttpClick()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl(baseUrl) .build(); return retrofit; } private OkHttpClient setOkHttpClick() { OkHttpClient okHttpClient=new OkHttpClient().newBuilder() .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .writeTimeout(15,TimeUnit.SECONDS) .addInterceptor(new HttpLoggingInterceptor()) .retryOnConnectionFailure(true) .build(); return okHttpClient; } }
android Retrofit简单封装
最新推荐文章于 2024-06-27 23:57:40 发布