我在服务器中使用SpringBoot。我有一种方法可以这样调用:
@SuppressWarnings("deprecation")
@PostMapping(path = "/checkLoginMobile")
public ResponseEntity<UserAccountRespone> loginMobile(@RequestBody String account) throws ParseException {
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(account);
String username = json.get("username").toString();
String password = json.get("password").toString();
UserAccount userAccount = userAccountRepository.login(username, password);
UserProfile userProfile = userProfileRepository.getUserProfile(username);
if (userAccount != null) {
UserAccountRespone userAccountRespone = new UserAccountRespone(username, userProfile.getFullname(),
userProfile.getBirth(), userProfile.getAddress(), userProfile.getCity(), userProfile.getEmail(),
userProfile.getPhone(), userProfile.getSexual(), userProfile.getCreatedAt(),
userProfile.getUpdatedAt(), userAccount.getRole().getId(), userAccount.getRole().getName());
return new ResponseEntity<UserAccountRespone>(userAccountRespone, HttpStatus.OK);
} else {
return new ResponseEntity<UserAccountRespone>(new UserAccountRespone(), HttpStatus.OK);
}
}
我有两个从移动设备调用api服务器的方法(使用Swift 4)。这是第一种方法:
func login(username: String, password: String, completion: @escaping (_ result: UserProfile?) -> ()){
var _userProfile:UserProfile?
let url = BASE_URL + "checkLoginMobile"
let parameters: Parameters = [
"username": username,
"password": password
]
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
debugPrint(response)
print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
switch response.result {
case .success:
if((response.result.value) != nil) {
let json = JSON(response.result.value!)
_userProfile = UserProfile(username: json["username"].string!,
fullname: json["fullname"].string!,
birth: json["birth"].int!,
address:json["address"].string!,
city:json["city"].string!,
email:json["email"].string!,
phone:json["phone"].string!,
roleId:json["roleId"].int!,
roleName:json["roleName"].string!,
sexual:json["sexual"].int!,
createdAt:json["createdAt"].int!,
updatedAt:json["updatedAt"].int!)
completion(_userProfile)
}
case .failure(let error):
print(error)
completion(nil)
}
}
}
第二种方法:
func getAllModels(completion: @escaping (_ result: [String]?) -> ()) {
var _modelList:[String] = []
let url = BASE_URL + "getAllProductAndModelv2"
Alamofire.request(url, method:.get, parameters: [:], encoding: JSONEncoding.default).responseJSON { response in
switch response.result {
case .success:
if((response.result.value) != nil) {
let data = NSData(contentsOf: URL(string: url)!)
do {
if let data = data, let json = try JSONSerialization.jsonObject(with: data as Data) as? [String: Any], let models = json["all"] as? [[String:Any]] {
for model in models {
if let name = model["link"] as? String {
_modelList.append(name)
}
}
}
}catch {
print("error")
}
completion(_modelList)
}
case .failure(let error):
print(error)
completion(nil)
}
}
}
流程首先是我从移动设备运行getAllModel()
方法,它将从服务器调用loginMobile()
api。接下来,我从移动设备运行login()
方法,它将从服务器调用loginMobile()
api。
当我的移动应用程序调用{{1}}方法,应用程序崩溃和服务器返回错误时,错误原因是:
login()
我不知道为什么这个方法&#39; POST&#39;成为ST&#39; ST&#39;方法... 如何解决这个问题?