Flutter GetX的GetConnect在安卓上无法连接到HTTP。

huangapple go评论101阅读模式
英文:

Flutter GetX's GetConnect not connecting to http on android

问题

I have a Flutter/GetX project that successfully calls http REST APIs from iOS, but does not work from Android. My AndroidManifest.xml file appears as follows:

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:label="vending_consumer"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
        android:usesCleartextTraffic="true">
        ....
    </application>
</manifest>

So what this should mean is:

  • 应用程序已被授予互联网访问权限
  • 应用程序启用了明文 http

Unfortunately, the REST API calls do not work on Android... but work just fine on iOS.

I am using Flutter and GetX, so my provider class appears as follows:

class ConsumerProvider extends GetConnect {
  late String token;
  late Map<String, String> _mainHeaders;

  ConsumerProvider() {
    timeout = const Duration(seconds: 30);
    _mainHeaders = {
      'accept': '*/*',
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Request-Method': 'GET',
      'Access-Control-Request-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
    };
  }

  @override
  void onInit() {
    super.onInit();
    httpClient.baseUrl = 'http://localhost:7001/';
    httpClient.defaultContentType = "application/json";
    httpClient.timeout = const Duration(seconds: 8);
  }

  Future<Consumer> getConsumerByMobileNumber(String mobileNumber) async {
    String url = 'enterprise/masterdata/party/consumers/byMobileNumber/$mobileNumber';
    final Response response = await get(url);
    if (response.status.hasError) {
      debugPrint('[ConsumerProvider getConsumerByMobileNumber] status: ${response.status.code}');
      debugPrint('[ConsumerProvider getConsumerByMobileNumber] response: ${response.body}');
      return Future.error(response.body);
    } else {
      return Consumer.fromJson(response.body);
    }
  }
  ....
}

The response body returned by the GET API is simply null on Android. The server is never invoked. This does, however, work just fine on iOS.

Curious if there are additional Flutter or GetX configurations I am missing? Feedback is certainly appreciated.

英文:

I have a Flutter/GetX project that successfully calls http REST APIs from iOS, but does not work from Android. My AndroidManifest.xml file appears as follows:

    &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        package=&quot;com.example.vending_consumer&quot;&gt;
    
        &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
        &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
        &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
        &lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;
        &lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
    
        &lt;application
            android:label=&quot;vending_consumer&quot;
            android:name=&quot;${applicationName}&quot;
            android:icon=&quot;@mipmap/ic_launcher&quot;
            android:usesCleartextTraffic=&quot;true&quot;&gt;
            ....
        &lt;/application&gt;
    &lt;/manifest&gt;

So what this should mean is:

  • The application has been granted internet access permissions
  • The app is enabled for clear text http

Unfortunately, the REST API calls do not work on Android... but work just fine on iOS.

I am using Flutter and GetX, so my provider class appears as follows:

    class ConsumerProvider extends GetConnect {
    late String token;
    late Map&lt;String, String&gt; _mainHeaders;
    
    ConsumerProvider() {
        timeout = const Duration(seconds: 30);
        _mainHeaders = {
            &#39;accept&#39;: &#39;*/*&#39;,
            &#39;Access-Control-Allow-Origin&#39;: &#39;*&#39;,
            &#39;Access-Control-Request-Method&#39;: &#39;GET&#39;,
            &#39;Access-Control-Request-Headers&#39;: &#39;Origin, X-Requested-With, Content-Type, Accept&#39;,
            };
        }
    
    @override
    void onInit() {
        super.onInit();
        httpClient.baseUrl = &#39;http://localhost:7001/&#39;;
        httpClient.defaultContentType = &quot;application/json&quot;;
        httpClient.timeout = const Duration(seconds: 8);
    }
    
    Future&lt;Consumer&gt; getConsumerByMobileNumber(String mobileNumber) async {
        String url = &#39;enterprise/masterdata/party/consumers/byMobileNumber/$mobileNumber&#39;;
        final Response response = await get(url);
        if (response.status.hasError) {
            debugPrint(&#39;[ConsumerProvider getConsumerByMobileNumber] status: ${response.status.code}&#39;);
            debugPrint(&#39;[ConsumerProvider getConsumerByMobileNumber] response: ${response.body}&#39;);
            return Future.error(response.body);
        } else {
            return Consumer.fromJson(response.body);
        }
        }
        ....
    }

The response body returned by the GET API is simply null on Android. The server is never invoked. This does, however, work just fine on iOS.

Curious if there are additional Flutter or GetX configurations I am missing? Feedback is certainly appreciated.

答案1

得分: 1

问题是在我的URL中使用了“localhost”。iOS模拟器可以理解这个,但Android模拟器不能。一旦将“localhost”替换为“192.168.0.x”,一切都正常。

英文:

The issue is using "localhost" in my URL. The iOS Simulator is able to make sense of this, but not the Android Emulator. Once replacing "localhost" for "192.168.0.x", all works fine.

huangapple
  • 本文由 发表于 2023年5月24日 21:53:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定