ios swift 3 Alamofire 发布请求问题

Posted

技术标签:

【中文标题】ios swift 3 Alamofire 发布请求问题【英文标题】:ios swift 3 Alamfire post request issue 【发布时间】:2017-09-20 11:03:28 【问题描述】:

enter image description hereenter image description here在此处输入图片描述我正在使用 Alamofire。我被困在发布帖子请求中。 我有一个帖子正文 -

[
    
    "siteName": "lab1",
    "locationCode": "111",
    "locationName": "test1"
    
]

我应该如何拨打请求电话?我在做 -

    let parameters: Parameters = [
        "siteName": "lab",
        "locationCode": "1156",
        "locationName": "123test"
    ]
    Alamofire.request(URLStr, method: .post, parameters: parameters , encoding: JSONEncoding.default, headers: headers).responseJSON  response in
        print("Request: \(String(describing: response.request))")   // original url request
        print("Response: \(String(describing: response.response))") // http url response
        print("Result: \(response.result)")                         // response serialization result

        if let json = response.result.value 
            print("JSON: \(json)") // serialized json response
            sucessHandler(json)
        

        if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) 
            print("Data: \(utf8Text)") // original server data as UTF8 string
            failureHandler(response.error)
        
    

【问题讨论】:

什么问题? 在你的代码中有一个错字错误parameters: params应该是parameters: parameters in Alamofire.request 您返回的状态码是什么?您可以使用response.response?.statusCode 打印它 它在访问服务器后给出以下错误-- json value ["status": 400, "exception": org.springframework.http.converter.HttpMessageNotReadableException, "message": JSON parse error: Can不从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例;嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:无法从 [Source: java.io.PushbackInputStream@2104e118; 的 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例;行:1,列:1],“时间戳”:1505905556480,“路径”:/Configuration/saveStoreLocationDetails/,“错误”:错误请求] 你检查params对象了吗?应该是parameters 【参考方案1】:

非常感谢你们。我找到了另一种方法。

让 fileUrl = NSURL(string: URLStr)

    var request = URLRequest(url:fileUrl as! URL )
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    let values = [parameters]

    request.httpBody = try! JSONSerialization.data(withJSONObject: values)

    Alamofire.request(request)
        .responseJSON  response in
            // do whatever you want here
            switch response.result 
            case .failure(let error):
                print(error)

                if let data = response.data, let responseString = String(data: data, encoding: .utf8) 
                    print(responseString)
                    failureHandler(response.error)

                
            case .success(let responseObject):
                print(responseObject)
                do 
                    let user = try IeroLocationSave(JSONDecoder(response.data ?? "nil..12"))
                    //print("city is: \(user.address.city)")
                    sucessHandler(user)
                    //That's it! The object has all the appropriate properties mapped.
                 catch 
                    print("unable to parse the JSON")
                

            
    

【讨论】:

【参考方案2】:

下面的代码对我有用

let parameters: Parameters = ["feedback_name": "SwiftTest","feedback_email":"m@m.com","feedback_description":"Test"]

        Alamofire.request("http://212.69.45.77:8082/api/feedbackapp",method: .post,parameters: parameters).responseJSON  response in
            print("Request: \(String(describing: response.request))")   // original url request
            print("Response: \(String(describing: response.response))") // http url response
            print("Result: \(response.result)")                         // response serialization result

            if let json = response.result.value 
                print("JSON: \(json)") // serialized json response
            

            if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) 
                print("Data: \(utf8Text)") // original server data as UTF8 string
            
        

【讨论】:

以上是关于ios swift 3 Alamofire 发布请求问题的主要内容,如果未能解决你的问题,请参考以下文章

IOS Swift 3 Alamofire 4.0.0

如何在 Alamofire Swift iOS 中将 Origin 传递给 Header?

Swift 3 的 ObjectMapper 和 Alamofire 问题 Alamofire 4 的 Alamofire 版本

Swift AlamoFire 5 不会执行请求

无法解析响应 alamofire swift 3

Swift 3 / Alamofire:无法用数据加载表格