android的Retrofit请求https://192.168.43.73:8080/报错:Handshake failed

1.报错原因:Handshake failed(握手失败)

2.原来的url地址为:

java 复制代码
    //这里加了s(https)
    private static final String BASE_URL = "https://192.168.43.73:8080/";  // 替换为你的接口域名

要更改为:

java 复制代码
    //这里没加s(http)
    private static final String BASE_URL = "http://192.168.43.73:8080/";  // 替换为你的接口域名

这两者的区别是:一个加s(https),一个没加s(http)。要改成不加s

3.需要开启明文HTTP(仅测试环境)

从Android 9.0 (API 28) 开始,系统默认禁止明文HTTP请求。

第一步 在res/xml 目录创建

ggh.xml 文件 :

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">192.168.43.73</domain> <!-- 你的测试服务器IP -->
    </domain-config>
</network-security-config>

在 AndroidManifest.xml 中引用该配置文件:

java 复制代码
<application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"

        //在这里引用ggh,这句话需要删除,现在只是提示给你看
        android:networkSecurityConfig="@xml/ggh"

        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication001"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

4.添加网络权限

在 AndroidManifest.xml 中确保已添加网络权限:

java 复制代码
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

5.再次重新安装运行app,发现已经可以正常请求url地址了,并且有数据返回给安卓端

相关推荐
2601_962066499 小时前
【Sql Server】Update中的From语句,以及常见更新操作方式
android·java·数据库
用户693717500138415 小时前
曾经安卓开发人手一个的 EventBus,为什么现在没人爱用了?
android·android studio
九皇叔叔16 小时前
MySQL ORDER BY 性能优化详解:Using filesort、联合索引、ASC/DESC 与覆盖索引
android·adb
OriginCoding17 小时前
用 AI Agent 协作完成一个 Android TOTP 应用:从需求边界到 v1.0.0
android·ai编程
杉氧19 小时前
React 灵魂:深入剖析 Hooks 与副作用(Side Effects)陷阱
android·前端·react native
又见情义20 小时前
RK3568 + RTL8211F 网络唤醒(WOL)功能适配全记录
android·网络·驱动开发
用户09340777351420 小时前
HarmonyOS WPS Open SDK 实践:registerApp 鉴权与就绪门禁
android·typescript·harmonyos
jike_202620 小时前
会议离线录音APP:无网络记录能力对比
android·智能手机·语音识别
always_TT21 小时前
【Python 字符串格式化:format() 方法】
android·开发语言·python
恋猫de小郭1 天前
AI 时代,一个优化 Flutter 的重复代码工具 Deslop
android·前端·flutter