首页 > 解决方案 > Flutter在某些设备上无法连接到服务器在Release模式下

问题描述

我发布了应用程序,但在某些设备上遇到错误 (Android)

在某些设备上,应用程序无法连接到 Web 服务 (API) 来发送或接收信息 Android 版本会出现此问题

我也把 Android Permission 放在了 AndroidManifest 但是它不起作用

设备之一:三星 Galaxy A10 | 安卓 8

此设备已安装此版本:v7a

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.test">

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />

<queries>
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
  </intent>
</queries>

<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="test"
    android:usesCleartextTraffic="true"
    android:networkSecurityConfig="@xml/network_security_config"
    android:requestLegacyExternalStorage="true"
    android:icon="@mipmap/ic_launcher">

    <activity
      android:name="com.yalantis.ucrop.UCropActivity"
      android:screenOrientation="portrait"
      android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
   
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        
        <meta-data
          android:name="io.flutter.embedding.android.NormalTheme"
          android:resource="@style/NormalTheme"
          />
        
        <meta-data
          android:name= "io.flutter.embedding.android.SplashScreenDrawable"
          android:resource="@drawable/launch_background"
          />
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        

        <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
          <!-- Accepts URIs that begin with YOUR_SCHEME://YOUR_HOST -->
          <data android:scheme="test" android:host="transaction-ok" />
          <data android:scheme="test" android:host="transaction-faild" />
        </intent-filter>


    </activity>
    <!-- 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" />
</application>
</manifest>

网络安全配置: https ://i.stack.imgur.com/jZGdL.png

和我的代码:

static Future loginCode(String phone) async {

    String url = 'https://panel.test.ir/api/v1/login_code/lawyer';

    try{
      var response = await http.post(
        url,
        body: {
          "mobile_number": phone
        },
        headers: {
          accept : acceptValue
        }
      ).timeout(Duration(seconds: 30));
      print(response.statusCode);

      var data = jsonDecode(response.body);

      try{
        showToast(data['message']);
      }catch(e){}

      if(response.statusCode == 200){
    
        if(data['new_user'] == null){
          return false;
        }else{
          return true;
        }
  
      }else{
        showError(response);
        return null;
      }
    }catch(e){
      showToast("Send Data Error");
      return null;
    }
}

30秒后,请求会自动取消

安卓 -> 应用程序 -> BG:图像

标签: javaandroidflutterapidart

解决方案


推荐阅读