[英]How to fix SSL handshake timed out in Retrofit
In my application I want get data from server and show this into RecyclerView
. 在我的应用程序中,我想从服务器获取数据并将其显示到
RecyclerView
。
For get data from server I use Retrofit2 and I write below codes. 为了从服务器获取数据,我使用Retrofit2,并编写以下代码。
But when running application after some time show me E/priceResLog: Err : SSL handshake timed out
in onFailure
from Retrofit2 ! 但一段时间后,运行应用程序时,向我展示
E/priceResLog: Err : SSL handshake timed out
在onFailure
从Retrofit2!
My codes : 我的代码:
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 codes : 活动代码:
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());
}
});
}
How can I fix it? 我该如何解决? please help me thanks.
请帮我谢谢。
Add the following piece of code 添加以下代码
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();
}
or change your code to 或将您的代码更改为
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;
}
Create another class called HttpClientService and add the following code 创建另一个名为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);
}
}
}
Replace your ApiUtilsPrice Price with the class given below: 将您的ApiUtilsPrice价格替换为以下类别:
public class RetroFitSingleton {
private static final String TAG = RetroFitSingleton.class.getSimpleName();
private static final String SERVER_URL = "SERVER_URL/A";
private static Retrofit retrofit;
private static RetroInterface retroInterface;
public static RetroInterface getInstance(Activity activity) {
initializeRetroFit(activity);
return retroInterface;
}
private static void initializeRetroFit(Activity activity) {
if (retrofit == null) {
OkHttpClient okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.readTimeout(60, TimeUnit.SECONDS);
httpClient.connectTimeout(60, TimeUnit.SECONDS);
httpClient.addInterceptor(logging);
retrofit = new Retrofit.Builder().client(httpClient.build()).addConverterFactory(GsonConverterFactory.create()).baseUrl(SERVER_URL).client(okHttpClient).build();
retroInterface = retrofit.create(RetroInterface.class);
}
} }
Import the file given below into your code: 将下面给出的文件导入您的代码中:
public class UnsafeOkHttpClient {
public static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager)trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.