为什么我看不到响应值?

时间:2019-09-13 15:50:50

标签: ios swift networking

我是DataGridView的初学者。我现在通过网络通信收到iOS。目前,我的代码已经调试过,但是还不属于response

发布功能

catch

通话功能

    func post(url: URL, body: NSMutableDictionary, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) throws {

        var postRequest: URLRequest = URLRequest(url: url)
        postRequest.httpMethod = "POST"
        postRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
        postRequest.addValue("application/json", forHTTPHeaderField: "Accept")
        postRequest.addValue("*******", forHTTPHeaderField: "api_key")
        postRequest.addValue("*******", forHTTPHeaderField: "game_key")
        postRequest.httpBody = try JSONSerialization.data(withJSONObject: body, options: JSONSerialization.WritingOptions.prettyPrinted)
        session.dataTask(with: postRequest, completionHandler: completionHandler).resume()

    }

我无法检查任何值。

预先感谢

****************************编辑****************** *****************

啊,我做错了Debug。我可以看到数据值。但是值是字节。无法查看实际值。

登录


72个字节// // Log.Debug(data)

Log.Debug(响应)

    @IBAction func nextScreen(_ sender: Any) {
        if self.nickField.text!.count > 12 {
            callAlert(DacAlert.overNickname.alertResult!)
        } else {

            let url: URL = URL(string: globalUrl.apiAddress + globalUrl.ApiValue + globalUrl.ApiValueSecond2)!
            let body: NSMutableDictionary = NSMutableDictionary()


            Address = LocalStorage.get("Address") as? String

            body.setValue(self.nickField.text!, forKey: "new_nickname")
            body.setValue(Address, forKey: "address")

            do {
                try globalUrl.post(url: url, body: body, completionHandler: { data, response, error in
                    Log.Debug("**********************************************")
                    Log.Debug(response)
                    Log.Debug("**********************************************")
                    Log.Debug(data)
                    Log.Debug("**********************************************")
                    Log.Debug(error)
                    Log.Debug("**********************************************")
                })
            } catch {
                Log.Debug("**********************************************")
                print(error)
            }

        }
    }

正常响应值

<NSHTTPURLResponse: 0x600003aa8420> { URL: http://11.111.111.11:8080/***** } { Status Code: 200, Headers {
    "Content-Type" =     (
        "application/json;charset=UTF-8"
    );
    Date =     (
        "Fri, 13 Sep 2019 16:03:40 GMT"
    );
    "Set-Cookie" =     (
        "JSESSIONID=F8CC56CA656E11CB7EEA7B51D8D3810E; Path=/; HttpOnly"
    );
    "Transfer-Encoding" =     (
        Identity
    );
} }

出什么问题了?

1 个答案:

答案 0 :(得分:1)

我能够通过制作JSON来解决数据。

do {
    let json = try JSONSerialization.jsonObject(with: data!, options: [])
    Log.Debug(json)
} catch {
    Log.Error(error)
}