【发布时间】:2019-05-23 11:26:03
【问题描述】:
当我连接到服务器以检查用户名和密码时,就像这个结构一样
{
"Username":"1",
"Password":"1"
}
我得到0或1,没有任何json格式,用户名和密码的第一个字母是大写字母,Username,Password,
现在我想用颤振和post http 动词得到这个响应
UserInformation类
class UserInformation {
String Username;
String Password;
UserInformation(this.Username, this.Password);
Map<String, dynamic> toJson() {
final Map<String, dynamic> json = new Map<String, dynamic>();
json['Username'] = this.Username;
json['Password'] = this.Password;
return json;
}
}
LoginRepository类:
class LoginRepository {
Future<int> authenticate(
{@required String username, @required String password}) async {
UserInformation userInformation = UserInformation(username, password);
/*this line print {Username: 1, Password: 1}*/
print(userInformation.toJson().toString());
final response = await http.post(
Constants.loginApi, body: userInformation.toJson());
final responseString = jsonDecode(response.body);
if (response.statusCode == 200) {
return responseString;
} else {
throw Exception('fail to get response');
}
}
}
然后这个输出是来自服务器的响应:
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FormatException: Unexpected character (at character 1)
<?xml version="1.0" encoding="utf-8"?>
【问题讨论】: