在 ios 中使用 POST API 使用 URLSession

using POST API in ios using URLSession

我现在正在学习 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 End-point

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.";
}