您可以通过创建自定义 HTTPStack 并在Volley.javaVolley.newRequestQueue(context, httpStack)
的方法中设置堆栈来修改 Volley 中使用的 TLS 版本。虽然,您只需要为 Android 版本 16-19 执行此操作。在 v16 之前,不支持 TLS 1.2,在 v19 之后,默认启用 TLS 1.2。因此,您应该专注于为 Android 版本 16-19 手动将 TLS 设置为 1.2。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
try {
ProviderInstaller.installIfNeeded(getContext());
} catch (GooglePlayServicesRepairableException e) {
// Indicates that Google Play services is out of date, disabled, etc.
// Prompt the user to install/update/enable Google Play services.
GooglePlayServicesUtil.showErrorNotification(e.getConnectionStatusCode(), getContext());
// Notify the SyncManager that a soft error occurred.
syncResult.stats.numIOExceptions++;
return;
} catch (GooglePlayServicesNotAvailableException e) {
// Indicates a non-recoverable error; the ProviderInstaller is not able
// to install an up-to-date Provider.
// Notify the SyncManager that a hard error occurred.
syncResult.stats.numAuthExceptions++;
return;
}
HttpStack stack = null;
try {
stack = new HurlStack(null, new TLSSocketFactory());
} catch (KeyManagementException e) {
e.printStackTrace();
Log.d("Your Wrapper Class", "Could not create new stack for TLS v1.2");
stack = new HurlStack();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
Log.d("Your Wrapper Class", "Could not create new stack for TLS v1.2");
stack = new HurlStack();
}
requestQueue = Volley.newRequestQueue(context, stack);
} else {
requestQueue = Volley.newRequestQueue(context);
}
然后使用扩展 SSLSocketFactory 的 TLSSocketFactory 类,就像 Florian Krauthan 在此处创建的那样,其中启用了 v1.2 TLS 协议:https ://gist.github.com/fkrauthan/ac8624466a4dee4fd02f#file-tlssocketfactory-java