【问题标题】:Alamofire .post in Swift. "Result of call is unused, but produces 'DataRequest'Swift 中的 Alamofire .post。 “调用结果未使用,但产生‘DataRequest’
【发布时间】:2017-02-05 08:58:03
【问题描述】:

尝试发布一些 XML。创建了一个 IBAction“SelectButtonA”,它使用 Alamofire.request 发布到一个地址。由结构处理的肥皂编码。

不起作用。我如何测试它是否正在发布,并解决错误“调用结果未使用,但产生'DataRequest'?

// 定义 Alamofire 的 SOAP 编码和 XML 的主结构。 //

struct SOAPEncoding: ParameterEncoding {
    let service: String
    let action: String
    let IRCCC: String = "AAAAAQAAAAEAAABlAw=="

    func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
        var urlRequest = try urlRequest.asURLRequest()

        guard parameters != nil else { return urlRequest }

        if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
            urlRequest.setValue("text/xml", forHTTPHeaderField: "Content-Type")
        }

        if urlRequest.value(forHTTPHeaderField: "SOAPACTION") == nil {
            urlRequest.setValue("\(service)#\(action)", forHTTPHeaderField: "SOAPACTION")
        }

        //let soapArguments = parameters.map({key, value in "<\(key)>\(value)</\(key)>"}).joined(separator: "")

        let soapMessage =
            "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" +
                "<s:Body>" +
                "<u:\(action) xmlns:u='\(service)'>" +
                IRCCC +
                "</u:\(action)>" +
                "</s:Body>" +
        "</s:Envelope>"

        urlRequest.httpBody = soapMessage.data(using: String.Encoding.utf8)

        return urlRequest
    }
}

@IBAction func SelectButtonA(sender: NSButton) {
    Alamofire.request("http://192.168.2.7/sony/IRCC?", method: .post, parameters: ["parameter" : "value"], encoding: SOAPEncoding(service: "urn:schemas-sony-com:service:IRCC:1", action: "X_SendIRCC"))
}

Screenshot of the whole thing. Trying to make a remote.

【问题讨论】:

    标签: xml swift interface-builder cocoapods alamofire


    【解决方案1】:

    这一切都在 GitHub 上的 Alamofire README 中进行了解释。

    Alamofire.request() 方法返回一个Request 对象并开始运行。然后,您可以在其上使用.response() 函数来获取该请求的结果。

    Alamofire.request(...).response {
        response in
    
        // get details of the result out of the response input to this closure.
    }
    

    第一部分...你怎么知道它有效?访问完成中的数据。

    第二部分...你如何摆脱错误?添加补全。

    【讨论】:

    • 我检查了响应。它正在产生一个格式错误的请求。我修好了。
    【解决方案2】:

    “调用结果未使用,但产生一个'DataRequest'只是一个警告消息,因为Alamofire.request(...)返回一个有价值的但你没有使用它,而不是错误消息。

    【讨论】:

      【解决方案3】:

      产生了一个格式错误的请求。

      用于发送 SOAP 请求的代码,没有标头等:

          Alamofire.request("http://192.168.2.7/sony/IRCC?",
                            method: .post,
                            parameters: ["parameter" : "value"],
                            encoding: SOAPEncoding(service: "urn:schemas-sony-com:service:IRCC:1",
                                                   action: "X_SendIRCC", IRCCC: "AAAAAQAAAAEAAABgAw==")).responseString { response in
                                                      print(response)
      

      【讨论】:

        猜你喜欢
        • 2022-08-09
        • 1970-01-01
        • 1970-01-01
        • 2020-02-10
        • 2021-01-26
        • 2015-11-23
        • 2018-02-10
        • 1970-01-01
        • 2016-11-06
        相关资源
        最近更新 更多