gpt4 book ai didi

ios - 使用 URLSession 在 ios 中使用 POST API

转载 作者:行者123 更新时间:2023-11-28 23:31:22 26 4
gpt4 key购买 nike

我现在正在学习IOS,我写的代码与教程完全一样。但我遇到了这样的错误

"2019-05-29 14:01:25.974883+0900 URLSessionJSONRequests[18165:938499] Task <0EC532CF-8CA6-42C9-9BD8-6D6E74BB9C06>.<10> finished with error - code: -1002"

我的代码是这样的。

@IBAction func onPostTab(_ sender: UIButton) {

let parameters = ["id":"uuzaza@naver.com","pw":"1q2w3e4r"]

guard let url = URL(string:"https//taky.co.kr/login/app_login") else{return}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: [])else{
return}
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)

}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [] )
print(json)
}catch{
print(error)
}
}
}.resume()


}

错误代码-1002是什么??我使用了占位符 API,它仍然受支持,我认为服务器不是问题。任何引用或建议将不胜感激!

最佳答案

请先检查 URL,您在 https 之后漏掉了“:”。

只需替换您的 API 端点

https//taky.co.kr/login/app_login

https://taky.co.kr/login/app_login

完成上述更改后一切正常。

API 响应:

<NSHTTPURLResponse: 0x60000327d120> { URL: https://taky.co.kr/login/app_login } { Status Code: 200, Headers {
"Cache-Control" = (
"no-store, no-cache, must-revalidate"
);
Connection = (
"Keep-Alive"
);
"Content-Encoding" = (
gzip
);
"Content-Length" = (
93
);
"Content-Type" = (
"text/html; charset=UTF-8"
);
Date = (
"Wed, 29 May 2019 05:32:58 GMT"
);
Expires = (
"Thu, 19 Nov 1981 08:52:00 GMT"
);
"Keep-Alive" = (
"timeout=5, max=100"
);
Pragma = (
"no-cache"
);
Server = (
"Apache/2.4.18 (Ubuntu)"
);
"Set-Cookie" = (
"cookie=9f73o1c13mggukb15fq25usfpc6ms6cp; expires=Wed, 29-May-2019 09:32:58 GMT; Max-Age=14400; path=/; HttpOnly"
);
Vary = (
"Accept-Encoding"
);
} }
{
code = E01;
message = "\Uc544\Uc774\Ub514\Ub97c \Uc785\Ub825\Ud574 \Uc8fc\Uc138\Uc694.";
}

关于ios - 使用 URLSession 在 ios 中使用 POST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56353427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com