gpt4 book ai didi

java - 如何修复 Retrofit 中 SSL 握手超时

转载 作者:搜寻专家 更新时间:2023-11-01 08:21:04 25 4
gpt4 key购买 nike

在我的应用程序中,我想从服务器获取数据并将其显示到 RecyclerView 中。
为了从服务器获取数据,我使用 Retrofit2 并编写了以下代码。
但是当一段时间后运行应用程序时,显示 E/priceResLog: Err : SSL handshake timed out in onFailure from Retrofit2!

我的代码:

public class ApiUtilsPrice {

private static final String BASE_URL = "https://core.arzws.com/";
private static Retrofit retrofit = null;

public static Retrofit getClient() {

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build();

if (retrofit == null) {
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}

Activity 代码:

private void getCoinData() {
loaderLayout(true);
Call<GoldListResponse> call = apiInterface.getGoldPrice();
call.enqueue(new Callback<GoldListResponse>() {
@Override
public void onResponse(Call<GoldListResponse> call, Response<GoldListResponse> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
loaderLayout(false);
model.clear();
model.addAll(response.body().getGoldBoard());
coinAdapter.notifyDataSetChanged();
isSendApi = true;
}
}
}

@Override
public void onFailure(Call<GoldListResponse> call, Throwable t) {
Log.e("priceResLog", "Err : " + t.getMessage());
}
});
}

我该如何解决?请帮助我谢谢。

最佳答案

添加如下代码

public Retrofit getRetrofit(Gson gson) {
return new Retrofit.Builder()
.baseUrl(ZoneApplication.getContext().getString(R.string.feed_data_base_url))
.client(HttpClientService.getUnsafeOkHttpClient())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(new NullOnEmptyConverterFactory())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

}

或将您的代码更改为

public static Retrofit getClient() {

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build();

if (retrofit == null) {
retrofit = new Retrofit.Builder()
.client(HttpClientService.getUnsafeOkHttpClient())
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}

创建另一个名为 HttpClientService 的类并添加以下代码

public class HttpClientService {
public static OkHttpClient getUnsafeOkHttpClient() {

try {
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] chain,
String authType) {
//Do nothing
}

@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] chain,
String authType) {
//Do nothing
}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[0];
}
}};
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts,
new java.security.SecureRandom());

final SSLSocketFactory sslSocketFactory = sslContext
.getSocketFactory();

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory)
.hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}

}
}

关于java - 如何修复 Retrofit 中 SSL 握手超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50981413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com