gpt4 book ai didi

带有 url 参数的 Swift GET 请求

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

我正在构建一个调查,当用户回答问题时,我将答案附加到带有参数的 url 字符串,并向我的服务器发送一个获取请求。因此,对于每个答案,都会有一条记录,其中包含所选答案、时间戳和调查的唯一 ID。

我不确定执行此操作的最佳方法,但这是目前我所拥有的。

我创建 url 并查询项目。

var urlComponents: URLComponents {

let resultID = surveyQuestions.resultId
print("\(String(describing: resultID))")

let resultResponseID = surveyQuestions.questions[surveyResultResponseId]
print("\(String(describing: resultResponseID))")

let questionIndex = questionNumbers
print("\(String(describing: questionIndex))")

var urlComponents = URLComponents(string: "My String URL")

urlComponents?.queryItems = [
URLQueryItem(name: "surveyResultsId", value: "\(String(describing: resultID))"),
URLQueryItem(name: "surveyResultsResponseId", value: "\(String(describing: resultResponseID))"),
URLQueryItem(name: "questions", value: "\(questionIndex)"),
URLQueryItem(name: "selectedAnswer", value: "\(storedAnswer)")

]

let url = urlComponents?.url

print(url!.absoluteString as Any)

return urlComponents!
}

然后我构建发送请求。

func sendRequest(_ url: String, parameters: [String: String], completion: @escaping ([String: Any]?, Error?) -> Void) {

var components = URLComponents(string: url)!
components.queryItems = parameters.map { (key, value) in
URLQueryItem(name: key, value: value)
}

components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
let request = URLRequest(url: components.url!)

let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, // is there data
let response = response as? HTTPURLResponse, // is there HTTP response
(200 ..< 300) ~= response.statusCode, // is statusCode 2XX
error == nil else { // was there no error, otherwise ...
completion(nil, error)
return
}

let responseObject = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
completion(responseObject, nil)
print("This is the \(responseObject!)")
}
task.resume()
}

最后我在按下回答时调用发送请求。

@IBAction func answerPressed(_ sender: UIButton) {

if sender.tag == selectedAnswer {
questionNumbers += 1
}
storedAnswer = [sender.tag]
// storedAnswer.append(sender.tag)
print(storedAnswer)

sendRequest("\(urlComponents)", parameters: ["": ""]) { responseObject, error in
guard let responseObject = responseObject, error == nil else {
print(error ?? "Unknown error")
return
}


// use `responseObject` here
}

questionNumbers += 1
updateQuestion()
}

现在,当我运行它时,我会返回包含查询项的字符串,但是当我运行发送请求时,我会收到未知错误。我觉得好像我做错了什么。对于“在此处使用 responseObject”区域,我在其中放了什么。我有点困惑。另外,当我调用发送请求时,我应该在参数值中输入什么。现在它们只是参数:["": ""]。我觉得好像我很亲近。非常感谢任何帮助。

最佳答案

首先,您所做的工作比可能需要的要多得多。您正在将查询字符串参数编码到 URLComponents 中,这是正确的。然后,在您的发送中,您正在分解您的 URL 并解析出组件,然后重新编码它们。您还进行了很多力展开,这很脆弱并且隐藏了问题。

这是在适合我的 Playground 中简化的代码:

import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

let sampleURL = "https://someserver.com/somepath"

func sendRequest(_ url: URL, completion: @escaping ([String: Any]?, Error?) -> Void) {

let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, // is there data
let response = response as? HTTPURLResponse, // is there HTTP response
(200 ..< 300) ~= response.statusCode, // is statusCode 2XX
error == nil else { // was there no error, otherwise ...
completion(nil, error)
return
}
let responseObject = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any]
completion(responseObject, nil)
}
task.resume()
}

var urlComponents: URLComponents? {

let resultID = "resultID123"
let resultResponseID = "responseID456"
let questionIndex = "questionNumbers1"

var urlComponents = URLComponents(string: sampleURL)
urlComponents?.queryItems = [
URLQueryItem(name: "surveyResultsId", value: "\(String(describing: resultID))"),
URLQueryItem(name: "surveyResultsResponseId", value: "\(String(describing: resultResponseID))"),
URLQueryItem(name: "questions", value: "\(questionIndex)"),
URLQueryItem(name: "selectedAnswer", value: "\("storedAnswer1")")

]
return urlComponents
}

if let urlComponents = urlComponents, let url = urlComponents.url?.absoluteURL {
sendRequest(url) { (result, error) in
print("Got an answer: \(String(describing: result))")
}
}

当我针对返回有效 JSON 的服务器 URL 运行此命令时,我得到:

Got an answer: Optional(["image": {
href = "https://example.com";
}, "object_types": {
card = {
fields = {
};
pollable = 1;
};
}])

关于带有 url 参数的 Swift GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57963583/

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