【问题标题】:Swift 4 - Alamofire post request with httpbodySwift 4 - 使用 httpbody 的 Alamofire 发布请求
【发布时间】:2019-10-24 13:36:21
【问题描述】:

我有这样的 Alamofire 发布请求:

var jsonArrayOfDictionaries = [[AnyHashable: Any]]()

let user = appDelegate.username
let password = appDelegate.password

let url = webservice + "PostTasks"
let credential = URLCredential(user: user!, password: password!, persistence: .forSession)
let headers = ["Accept": "application/json;odata=verbose", "Content-type": "application/json;odata=verbose"]
let jsonData: Data? = try? JSONSerialization.data(withJSONObject: jsonArrayOfDictionaries, options: .prettyPrinted)

print(jsonData!)

//request.httpBody = jsonData

Alamofire.request(url, method: .post, headers: headers).authenticate(usingCredential: credential).responseJSON {
        (response) in
        switch response.result {
        case .success:
            if let value = response.result.value {

                print(value)

                OperationQueue.main.addOperation({

                    completion(true)

                })


            }else{

                print("There is error in the server response")

                completion(false)

            }

        case .failure (let error):

            print("The NTLM request error is: ", error.localizedDescription)

            completion(false)

        }

    }

我的问题是如何将 jsonData 变量添加到此请求的 httpbody 中?我已经研究过这个问题,所有的解决方案似乎都过时了。请帮忙!

这是 jsonArrayOfDictionaries 的填充方式:

var jsonArrayOfDictionaries = [[AnyHashable: Any]]()

        for i in 0..<cellHolder.count {

            var jsonDict = [AnyHashable: Any]()

            jsonDict["scheduleTaskID"] = cellHolder[i].scheduleTaskID

            jsonDict["task"] = cellHolder[i].task

            jsonDict["scheduledDate"] = cellHolder[i].scheduledDate

            jsonDict["actualDate"] = cellHolder[i].actualDate

            jsonDict["finishedDate"] = cellHolder[i].finishedDate

            jsonDict["selected"] = (cellHolder[i].selected) ? 1 : 0

            jsonDict["completedBy"] = appDelegate.username

            jsonDict["sortOrder"] = cellHolder[i].sortOrder

            jsonArrayOfDictionaries.append(jsonDict)

            jsonDict = [AnyHashable: Any]()

        }

它在一个循环中并被附加。

【问题讨论】:

  • 不相关但从不prettyPrint 任何将被发送到服务器的东西。服务器不在乎。
  • 您是否尝试将 JsonData 对象作为参数参数的一部分放入?像这样:Alamofire.request(url, method: .post, parameters:jsonData, headers: headers).authenticate(usingCredential: credential).responseJSON { (response) in
  • @LoupG 我收到此错误:无法转换“数据”类型的值?到预期的参数类型“参数?” (又名'可选>')
  • @user979331是的,这是预期的。参数对象应该是字典类型([String: Any])而不是数据类型。为什么不尝试使用 jsonArrayOfDictionaries 而不转换为数据?

标签: ios swift alamofire


【解决方案1】:

1.改变

let jsonData: Data? = try? JSONSerialization.data(withJSONObject: jsonArrayOfDictionaries, options: .prettyPrinted)

let jsonData = try JSONSerialization.jsonObject(with: jsonArrayOfDictionaries, options: []) as? [String : Any]

2。将 jsonData 添加到您的请求中

Alamofire.request(url, method: .post, headers: headers, parameters: jsonData).authenticate(usingCredential: credential).responseJSON {

【讨论】:

  • 我收到 jsonData 的警告:从 'Data' 转换为不相关的类型 '[String : Any]' 总是失败
  • 你是否也删除了: Data?
  • 是的,我做到了,我仍然收到错误:从“数据”转换为不相关的类型“[String : Any]”总是失败
  • 现在请检查,JSONSerialization.jsonObject
  • 新错误:无法使用类型为“(带有:[[AnyHashable:Any]],选项:[Any])”的参数列表调用“jsonObject”
猜你喜欢
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-06
  • 1970-01-01
  • 1970-01-01
  • 2020-09-30
相关资源
最近更新 更多