Flutter 如何发出正确的http请求?

Flutter 如何发出正确的http请求?,flutter,http,dart,Flutter,Http,Dart,我的代码似乎无法发出正确的http请求。每当我运行以下代码时 var url = Uri.https('datamall2.mytransport.sg', '/ltaodataservice/BusArrivalv2?BusStopCode=', {'BusStopCode': busStopCode}); final response = await http.get( url, headers: <String, String>{ 'Conten

我的代码似乎无法发出正确的http请求。每当我运行以下代码时

var url = Uri.https('datamall2.mytransport.sg', '/ltaodataservice/BusArrivalv2?BusStopCode=', {'BusStopCode': busStopCode});
  final response = await http.get(
    url,
    headers: <String, String>{
      'Content-Type': 'application/json',
      'AccountKey': accountKey,
    },
  );
这就是我试图发出的http请求

http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2?BusStopCode=busStopCode (busStopCode is a 5 digit number)

您应该将url变量更改为:

var url = Uri.http('datamall2.mytransport.sg', '/ltaodataservice/BusArrivalv2', {'BusStopCode': busStopCode});

使用了错误的协议

您已请求httpS。 服务器仅响应http

您可以尝试更简单的方法:

String url = 'http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2?$busStopCode';
  final response = await http.get(
    url,
    headers: <String, String>{
      'Content-Type': 'application/json',
      'AccountKey': accountKey,
    },
  );
String url='1!'http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2?$BUSTOPCODE';
最终响应=等待http.get(
网址,
标题:{
“内容类型”:“应用程序/json”,
“AccountKey”:AccountKey,
},
);

最新的http包需要Uri而不是字符串。在更新发布规范版本之前,我一直是这样做的。啊,在这种情况下:
Uri=Uri.parse('http://myrandom-url.com/with-stuff');可能是创建URI对象的一种更简单的方法。我得到错误:错误状态:平台不允许不安全的HTTP。哦,没关系,显然我需要添加以下权限:Android打开Android/app/src/main文件夹中的AndroidManifest.xml文件。然后将usesCleartextTraffic设置为true。iOS打开iOS/Runner文件夹中的Info.plist文件。然后添加以下键。NSAppTransportSecurity NSAllowsArbitraryLoads
String url = 'http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2?$busStopCode';
  final response = await http.get(
    url,
    headers: <String, String>{
      'Content-Type': 'application/json',
      'AccountKey': accountKey,
    },
  );