英文:
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:
<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:
- 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<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.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论