当前位置:   article > 正文

harmonyos域名,【HarmonyOS】网络请求

harmonyos sslsocketfactory

package com.example.demo.classone;

import com.example.demo.DemoAbilityPackage;

import ohos.app.Environment;

import okhttp3.*;

import retrofit2.Retrofit;

import java.io.File;

import java.util.concurrent.TimeUnit;

/**

* 提供获取Retrofit对象的方法

*/

public class ApiManager {

private static final String BUSINESS_BASE_HTTP_URL = "http://www.baidu.com";

private static Retrofit instance;

private static OkHttpClient mOkHttpClient;

private ApiManager(){}

public static Retrofit get(){

if (instance == null){

synchronized (ApiManager.class){

if (instance == null){

setClient();

instance = new Retrofit.Builder().baseUrl(BUSINESS_BASE_HTTP_URL).

addConverterFactory(ApiResponseConverterFactory.create()).client(mOkHttpClient).build();

}

}

}

return instance;

}

private static void setClient(){

if (mOkHttpClient != null){

return;

}

Cache cache = new Cache(new File(getRootPath(Environment.DIRECTORY_DOCUMENTS),"HttpCache"),1024*1024*100);

OkHttpClient.Builder builder = new OkHttpClient.Builder()

//                .followRedirects(false)//关闭重定向

//                .addInterceptor(new AppendUrlParamIntercepter())

.cache(cache)

.retryOnConnectionFailure(false)

.sslSocketFactory(SSLSocketClient.getSSLSocketFactory())

.hostnameVerifier(SSLSocketClient.getHostnameVerifier())

.readTimeout(8,TimeUnit.SECONDS)

.writeTimeout(8,TimeUnit.SECONDS)

.connectTimeout(8, TimeUnit.SECONDS);

//                .protocols(Collections.singletonList(Protocol.HTTP_1_1));

mOkHttpClient = builder.build();

mOkHttpClient.dispatcher().setMaxRequests(100);

}

private static String getRootPath(String dirs) {

String path = DemoAbilityPackage.getInstance().getCacheDir() + "/" + dirs;

File file = new File(path);

if (!file.exists()) {

file.mkdirs();

}

return path;

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/651611
推荐阅读
相关标签
  

闽ICP备14008679号