React Native + Expo构建的Android APK无法通过VPN访问指定服务器的问题求助
React Native + Expo构建的Android APK无法通过VPN访问指定服务器的问题求助
大家好,我刚用React Native和Expo完成了我的第一个Android应用开发,现在遇到了一个棘手的问题,想请各位帮忙分析下。
问题详情
- 我的应用需要通过VPN访问特定的内网服务器IP(目前还没有配置域名)
- 在Expo Go的开发模式下,应用能正常连接服务器并完成请求
- 但把应用打包成APK安装到手机后,点击请求按钮就会弹出Axios Network Error的提示
- 我确认两种场景下VPN都是正常连接状态,但怀疑打包后的APK没有走VPN通道
- 换了三台不同的Android手机测试,问题完全一致
已完成的排查操作
- 用手机上的PingTools工具,通过VPN成功ping通了目标服务器IP
- 给手机安装tcpdump抓包,运行APK发起请求后,完全看不到针对目标服务器的出站流量
核心代码示例
import { handleApiError } from "@/utils/handleApiError"; import { handleResponseError } from "@/utils/handleResponseErrors"; import axios from "axios"; import React from "react"; import { Alert, Button, View } from "react-native"; const getAuth = async () => { const axiosInstance = axios.create({ baseURL: "http://192.168.X.X", headers: { "Content-Type": "application/json" }, }); try { const response = await axiosInstance.post( "/api/Account/login", { userName: "TestUser", password: "1234", clientApp: "MobileApp", organizationId: null } ); if (response && response.status === 200) { const data = response.data; const user = `id: ${data.id}, email: ${data.email}, userName: ${data.userName}, firstName: ${data.firstName}, lastName: ${data.lastName}`; Alert.alert(`User: ${user}`); } else { const errorResult = handleResponseError(response); Alert.alert("Authentication error", errorResult.message); return errorResult; } } catch(error) { const errorResult = handleApiError(error); Alert.alert("Authentication error", errorResult.message || "Unknown error"); } } export default function Index() { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', }} > <Button title="Auth" onPress={getAuth} /> </View> ) }
我实在想不通为什么开发模式一切正常,打包成APK后就无法通过VPN访问服务器了,有没有前辈遇到过类似的问题?麻烦给点思路或者解决方案,万分感谢!
内容来源于stack exchange
相关产品推荐
相关产品推荐

