diff --git a/app/src/main/java/com/kunfei/bookshelf/base/BaseModelImpl.java b/app/src/main/java/com/kunfei/bookshelf/base/BaseModelImpl.java index a9bc0302ed..0236998f5a 100644 --- a/app/src/main/java/com/kunfei/bookshelf/base/BaseModelImpl.java +++ b/app/src/main/java/com/kunfei/bookshelf/base/BaseModelImpl.java @@ -32,7 +32,7 @@ import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; public class BaseModelImpl { - private static OkHttpClient.Builder clientBuilder; + private static OkHttpClient httpClient; public static BaseModelImpl getInstance() { return new BaseModelImpl(); @@ -66,7 +66,7 @@ public Retrofit getRetrofitString(String url) { .addConverterFactory(EncodeConverter.create()) //增加返回值为Observable的支持 .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) - .client(getClientBuilder().build()) + .client(getClient()) .build(); } @@ -76,13 +76,13 @@ public Retrofit getRetrofitString(String url, String encode) { .addConverterFactory(EncodeConverter.create(encode)) //增加返回值为Observable的支持 .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) - .client(getClientBuilder().build()) + .client(getClient()) .build(); } - public static OkHttpClient.Builder getClientBuilder() { - if (clientBuilder == null) { - clientBuilder = new OkHttpClient.Builder() + synchronized public static OkHttpClient getClient() { + if (httpClient == null) { + httpClient = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .writeTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) @@ -90,9 +90,10 @@ public static OkHttpClient.Builder getClientBuilder() { .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.createTrustAllManager()) .hostnameVerifier(SSLSocketClient.getHostnameVerifier()) .protocols(Collections.singletonList(Protocol.HTTP_1_1)) - .addInterceptor(getHeaderInterceptor()); + .addInterceptor(getHeaderInterceptor()) + .build(); } - return clientBuilder; + return httpClient; } private static Interceptor getHeaderInterceptor() { diff --git a/app/src/main/java/com/kunfei/bookshelf/utils/webdav/WebDav.kt b/app/src/main/java/com/kunfei/bookshelf/utils/webdav/WebDav.kt index 368c76efb4..284ee67b2a 100644 --- a/app/src/main/java/com/kunfei/bookshelf/utils/webdav/WebDav.kt +++ b/app/src/main/java/com/kunfei/bookshelf/utils/webdav/WebDav.kt @@ -130,7 +130,7 @@ constructor(urlStr: String) { ) } request.header("Depth", if (depth < 0) "infinity" else depth.toString()) - return BaseModelImpl.getClientBuilder().build().newCall(request.build()).execute() + return BaseModelImpl.getClient().newCall(request.build()).execute() } return null } @@ -225,7 +225,7 @@ constructor(urlStr: String) { Credentials.basic(it.user, it.pass) ) } - val response = BaseModelImpl.getClientBuilder().build().newCall(requestBuilder.build()).execute() + val response = BaseModelImpl.getClient().newCall(requestBuilder.build()).execute() return response.isSuccessful } @@ -236,7 +236,7 @@ constructor(urlStr: String) { request.header("Authorization", Credentials.basic(it.user, it.pass)) } try { - return BaseModelImpl.getClientBuilder().build().newCall(request.build()).execute().body?.byteStream() + return BaseModelImpl.getClient().newCall(request.build()).execute().body?.byteStream() } catch (e: IOException) { e.printStackTrace() } catch (e: IllegalArgumentException) {