Skip to content

Instantly share code, notes, and snippets.

@An0nymous0
Created April 26, 2019 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save An0nymous0/2cfce03eb511d052dedb3bd95dea46d5 to your computer and use it in GitHub Desktop.
Save An0nymous0/2cfce03eb511d052dedb3bd95dea46d5 to your computer and use it in GitHub Desktop.
[RetrofitApiSingleton] #Retrofit #okhttp #Singleton #DesignPattern
public class RetrofitApiSingleton {
private RetrofitApiSingleton() {
}
public static Retrofit getServiceMesh2ApiInstance() {
return ServiceMesh2ApiEnum.INSTANCE.getInstance();
}
@Getter
private enum ServiceMesh2ApiEnum {
INSTANCE;
private Retrofit retrofit;
ServiceMesh2ApiEnum() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.addInterceptor(logging);
this.retrofit = new Retrofit.Builder()
//设置数据解析器
.addConverterFactory(GsonConverterFactory.create())
//设置网络请求的Url地址
.baseUrl("http://service-mesh2:8081/service-mesh2/")
.client(httpClient.build())
.build();
}
private Retrofit getInstance() {
return this.retrofit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment