如何使用 Retrofit 的输入流加载 SSL 证书

How to load an SSL certificate using an input stream for Retrofit

我需要在 Android 中添加用于 Retrofit 的 SSL 证书,但我无法使用 Google 的示例代码找到该文件。

这是我正在使用 https://developer.android.com/training/articles/security-ssl

来自 Google 的示例

当运行:

val caInput: InputStream = BufferedInputStream(FileInputStream("rest_of_the_world_production.crt"))

我收到错误

 Caused by: java.io.FileNotFoundException: rest_of_the_world_production.crt: open failed: ENOENT (No such file or directory)

并且在尝试访问该文件时立即崩溃。该文件当前作为 crt 文件存储在 res/raw/rest_of_the_world_production.crt 下,为什么 Android 找不到它?

按如下操作:

InputStream cert = context.getResources().openRawResource(R.raw.my_cert); 
// Place your 'my_cert.crt' file in `res/raw`

原来我导入了

android.R

而不是

com.myApp.R

更改此允许我找到我的证书并导入它。