Created
April 26, 2019 03:03
-
-
Save An0nymous0/2cfce03eb511d052dedb3bd95dea46d5 to your computer and use it in GitHub Desktop.
[RetrofitApiSingleton] #Retrofit #okhttp #Singleton #DesignPattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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