You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android模拟器调用Dropbox API触发OpenSSLSocketFactoryImpl访问异常

问题分析

这个异常的核心原因是BouncyCastle依赖与Android系统内置的Conscrypt SSL实现发生类加载冲突。你引入的org.bouncycastle:bcpkix-jdk15on:1.67是适配JDK 1.5的旧版本,而Android 28+(你的minSdk为28)默认使用Conscrypt作为SSL底层实现,旧版BouncyCastle会干扰系统SocketFactory的类加载逻辑,导致跨类加载器的访问权限问题。

另外需要注意:你在主线程直接执行网络请求(client.users().getCurrentAccount())本身违反Android规范,不过当前崩溃的直接诱因是SSL类冲突。

解决方案

1. 修复BouncyCastle依赖冲突

从build.gradle的依赖列表中删除旧版BouncyCastle:

// 移除这一行
implementation 'org.bouncycastle:bcpkix-jdk15on:1.67'

如果代码确实需要BouncyCastle的加密功能,替换为Android适配的专用版本:

implementation 'org.bouncycastle:bcprov-android:1.70'
implementation 'org.bouncycastle:bcpkix-android:1.70'

这些版本针对Android环境做了优化,不会与系统SSL实现冲突。

2. 移到后台线程执行网络请求

Android禁止在主线程执行网络操作,否则会触发NetworkOnMainThreadException,推荐用Coroutine重构代码:

第一步:添加Coroutine依赖

在build.gradle的dependencies中加入:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'

第二步:修改Activity代码

import kotlinx.coroutines.CoroutineScope;
import kotlinx.coroutines.Dispatchers;
import kotlinx.coroutines.MainScope;
import kotlinx.coroutines.launch;
import kotlinx.coroutines.withContext;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private final CoroutineScope scope = MainScope();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.button.setOnClickListener(view -> testMe());
    }

    private void testMe(){
        // 在主线程启动协程,用于后续更新UI
        scope.launch(Dispatchers.Main) {
            String accountInfo = fetchDropboxAccount();
            // 更新UI,比如显示账户信息
            binding.textView.setText(accountInfo);
        }
    }

    // 在IO线程执行网络请求
    private String fetchDropboxAccount() {
        String clientId = String.format("%s/%s", BuildConfig.APPLICATION_ID, BuildConfig.VERSION_NAME);
        DbxRequestConfig config = new DbxRequestConfig(clientId);
        DbxClientV2 client = new DbxClientV2(config, BuildConfig.dropboxToken);
        try {
            return client.users().getCurrentAccount().toString();
        } catch (DbxException e) {
            e.printStackTrace();
            return "请求失败:" + e.getMessage();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        scope.cancel(); // 销毁时取消协程,避免内存泄漏
    }
}

3. 模拟器环境排查(可选)

如果上述操作后仍有问题,检查模拟器配置:

  • 确保使用带Google Play服务的系统镜像,而非纯Android镜像
  • 尝试更新模拟器到最新版本,或创建新的模拟器实例
验证方法

修改后重新编译运行,点击按钮后应能正常获取Dropbox账户信息,不会再出现SSL类加载异常。

内容的提问来源于stack exchange,提问作者Keen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 15:13:23