Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Jan 22, 2020
1 parent 51340b7 commit 94eff8d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 9 additions & 8 deletions app/src/main/java/com/kunfei/bookshelf/base/BaseModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -66,7 +66,7 @@ public Retrofit getRetrofitString(String url) {
.addConverterFactory(EncodeConverter.create())
//增加返回值为Observable<T>的支持
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(getClientBuilder().build())
.client(getClient())
.build();
}

Expand All @@ -76,23 +76,24 @@ public Retrofit getRetrofitString(String url, String encode) {
.addConverterFactory(EncodeConverter.create(encode))
//增加返回值为Observable<T>的支持
.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)
.retryOnConnectionFailure(true)
.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() {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/kunfei/bookshelf/utils/webdav/WebDav.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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) {
Expand Down

0 comments on commit 94eff8d

Please sign in to comment.