【发布时间】: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');
}
【问题讨论】: