【问题标题】:How to auto import and retrieve data from website with Swift code?如何使用 Swift 代码从网站自动导入和检索数据?
【发布时间】:2021-10-08 16:06:11
【问题描述】:

我有一个iphone序列号“F17TN84HHG7F”,我想用swift code自动进入这个网站字段:https://d3velopersteam.com/check

并将控制台收到的结果打印为:Model...Devide...Imei...enter code here:

我试过这段代码,但返回结果是:OOps not good JSON formatted response

let session = URLSession.shared
        let url = "https://d3velopersteam.com/check"
        let request = NSMutableURLRequest(url: NSURL(string: url)! as URL)
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        var params :[String: Any]?
        params = ["" : "F17TN84HHG7F"]
        do{
            request.httpBody = try JSONSerialization.data(withJSONObject: params as Any, options: JSONSerialization.WritingOptions())
            let task = session.dataTask(with: request as URLRequest as URLRequest, completionHandler: {(data, response, error) in
                if let response = response {
                    let nsHTTPResponse = response as! HTTPURLResponse
                    let statusCode = nsHTTPResponse.statusCode
                    print ("status code = \(statusCode)")
                }
                if let error = error {
                    print ("\(error)")
                }
                if let data = data {
                    do{
                        let jsonResponse = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
                        print ("data = \(jsonResponse)")
                    }catch _ {
                        print ("OOps not good JSON formatted response")
                    }
                }
            })
            task.resume()
        }catch _ {
            print ("Oops something happened buddy")
        }

希望大家帮我改写!!

【问题讨论】:

    标签: json swift post urlsession jsonresponse


    【解决方案1】:

    您能否尝试使用此代码来调试您的问题。

    func showResponse(_ data: Data) {
        if let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers), let jsonData = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) {
            print("\n---> json: " + String(decoding: jsonData, as: UTF8.self))
        } else {
            print("=========> json data malformed")
        }
    }
    

    在这里使用它:

            if let data = data {
            showResponse(data)   // <--- here
                do{
                    let jsonResponse = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
                    print ("data = \(jsonResponse)")
                }catch _ {
                    print ("OOps not good JSON formatted response")
                }
            }
                
    

    并告诉我们它打印的内容(您可以使用它来编辑您的问题)。

    【讨论】:

    • 很好,状态码=200,表示你的请求成功了。无需尝试 JSONSerialization 之类的其他方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    相关资源
    最近更新 更多