【问题标题】:How do I send parameters with an HTTP post request in Flutter?如何在 Flutter 中通过 HTTP 发布请求发送参数?
【发布时间】:2020-05-13 15:45:52
【问题描述】:

以前我可以在我的 Android 项目中使用 Retrofit 发送 HTTP 请求。

 @FormUrlEncoded
@POST("login")
Call<SignIn> loginUserWithoutKeyTest(@Field("User_Name") String name,
                                     @Field("Password") String password);

现在我将其转换为 Flutter。如何发出这个 HTTP 请求?

Folwing 是我的代码,但它不起作用。谁能告诉我正确的方法

 final http.Response response = await http.post(
    'https://xxxxxxxxxxxxxx.lk/login',
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(
        <String, String>{'User_name': '876378499', 'Password': '123456'}));
if (response.statusCode == 200) {
  // If the server did return a 201 CREATED response,
  // then parse the JSON.
  String data = response.body;
  print(data);
  var decodedData = jsonDecode(data);

  return decodedData;
} else {
  // If the server did not return a 201 CREATED response,
  // then throw an exception.
  throw Exception('Failed to load album');
}

【问题讨论】:

    标签: http flutter


    【解决方案1】:

    我刚刚删除了 JasonEncod 函数并制作如下。然后它工作了

    Future<dynamic> auliatestlogin() async {
    final http.Response response = await http
        .post('https://XXXXXXXXXXXXXXXXXXXXXXX/login', headers: <String, String>{
      'Content-Type': 'application/x-www-form-urlencoded',
    }, body: <String, String>{
      'User_Name': '077111',
      'Password': 'saman123'
    });
    if (response.statusCode == 200) {
      // If the server did return a 201 CREATED response,
      // then parse the JSON.
      String data = response.body;
      print(data);
      var decodedData = jsonDecode(data);
    
      return decodedData;
    } else {
      // If the server did not return a 201 CREATED response,
      // then throw an exception.
      throw Exception('Failed to load album');
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多