我正在使用 Flutter 创建跨平台应用程序,此错误仅发生在最新的 Android 设备上,例如 Galaxy S23 Ultra 和 Galaxy S24+。
Future<int> register(
String name,
String password,
String phoneNo,
String birthday,
String gender
) async {
var pref = await SharedPreferences.getInstance();
var token = pref.getString(SharedPreferencesKeys.fcmToken) ?? '';
Map data = {
'username': name,
'userbirth': birthday,
'gender': gender,
'phoneno': phoneNo,
'password': password,
'fcm': token,
};
var url = Uri.parse(Url.getSignupUrl());
var body = json.encode(data);
try {
var response = await http.post(
url,
headers: {
'Content-Type': 'application/json',
},
body: body,
).timeout(const Duration(seconds: 10));
if (response.statusCode == 200) {
Map<String, dynamic> decodedJson = json.decode(response.body); // for future use
String accessToken = decodedJson['result']['tokens']['accessToken']['accessToken'];
int accessTokenTimeout = decodedJson['result']['tokens']['accessToken']['timeout'];
String refreshToken = decodedJson['result']['tokens']['refreshToken']['refreshToken'];
int refreshTokenTimeout = decodedJson['result']['tokens']['refreshToken']['timeout'];
String userId = decodedJson['result']['userId'];
const storage = FlutterSecureStorage(
aOptions: AndroidOptions(
encryptedSharedPreferences: true,
)
);
print('register: $userId, $name, $phoneNo, $birthday, $sex');
storage.write(key: SecureStorageKeys.userId, value: userId);
storage.write(key: SecureStorageKeys.name, value: name);
storage.write(key: SecureStorageKeys.phoneNo, value: phoneNo);
storage.write(key: SecureStorageKeys.birthday, value: birthday);
storage.write(key: SecureStorageKeys.sex, value: sex);
await pref.setString(SharedPreferencesKeys.accessToken, accessToken);
await pref.setString(SharedPreferencesKeys.refreshToken, refreshToken);
await pref.setInt(SharedPreferencesKeys.accessTokenTimeout, accessTokenTimeout);
await pref.setInt(SharedPreferencesKeys.refreshTokenTimeout, refreshTokenTimeout);
return(response.statusCode);
} else {
return(-1);
}
} catch (e) {
return(-1);
}
}
上面是我遇到的代码。我在后端工作的同事说他没有收到任何请求。显然应该是客户的错误。
API 端点也是 http://WW.XXX.YYY.ZZZ:3000/my/route
我认为该代码可以正常工作,因为它适用于任何 iOS 设备和某些旧版本的 Android 设备(Galaxy Tab A7 Lite 和 Android Studio 中的 Android 模拟器 - 它们使用 Android 14)
我还在 main/res/xml/network_security_config.xml 添加了配置文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">WW.XXX.YYY.ZZZ</domain>
</domain-config>
</network-security-config>
这是我在 main 下的 Android 清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="mylabel"
android:name="${applicationName}"
android:extractNativeLibs="true"
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
android:showWhenLocked="true"
android:turnScreenOn="true"
>
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".VerificationActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
/>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<receiver
android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver"
android:exported="false" />
<receiver
android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
<!-- Tell Google Play Store that your app uses Bluetooth LE
Set android:required="true" if bluetooth is necessary -->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />
<!-- New Bluetooth permissions in Android 12
https://developer.android.com/about/versions/12/features/bluetooth-permissions -->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- legacy for Android 9 or lower -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />
</manifest>
Flutter Doctor 如果有人感兴趣的话
[✓] Flutter (Channel stable, 3.24.0, on macOS 14.2 23C64 darwin-arm64, locale ko-KR)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.94.2)
[✓] VS Code (version 1.91.1)
[✓] Network resources
有人知道我哪里错了吗?
我知道为什么了。我们的服务托管在只能通过办公室 WiFi 访问的隔离网络上。
测试人员在使用5G网络时尝试发出请求,但显然失败了。其他较旧的测试设备能够正常工作的原因是它们只能使用 wifi 互联网,因为它们没有连接到任何运营商服务。
这既不是前端问题,也不是后端问题,而是我遇到的最奇怪的错误之一,哈哈。