Alamofire 失败 - 响应序列化失败

问题描述 投票:0回答:1
func postAd(cryptoo: String, fiatt: String,locationn: String,countryy: String,marginn: String,
            pricee: String,minlimitt: String,maxlimitt: String,paymrntwindoww: String,paymrntmethodd: String,
            accountdetailss: String,infoo: String,termss: String
){
                    
    let endpoint = "https://new.demozab.com/armup/api/public/api/post-trade"
    
   // let params = ["email":"[email protected]","password":"Test@123"]
    
    let params = [
        
        "crypto_currency_id":"1",
        "trade_type": "buy",
        "location": "",
        "country": "1",
        "market":"15",
        "margin":"1",
        "trade_price":"1",
        "min_transaction_limit":"1",
        "max_transaction_limit":"1",
        "payment_method":"1",
        "payment_window":"1",
        "account_details": "1",
        "additional_information":"1",
        "sunday_start":"",
        "sunday_end":"",
        "monday_start":"",
        "monday_end":"",
        "tuesday_start":"",
        "tuesday_end":"",
        "wednesday_start":"",
        "wednesday_end":"",
        "thursday_start":"",
        "thursday_end":"",
        "friday_start":"",
        "friday_end": "",
        "saturday_start": "",
        "saturday_end": "",
        "terms":"i",
        
    ]
     guard let url = URL(string: endpoint)
    else {

        print("Invalid URL")
        return
    }
    let headers: HTTPHeaders = [
        "Authorization": "Bearer TVfXc1gPZbLfruJ53Q7PkkSDjSXi6gXGWXkCXg6j",
        "Content-Type": "application/json"
    ]



    AF.request( url, method: .post, parameters: params,  headers: headers ).responseDecodable(of: CommonData.self) {
        (response) in
       
      print(response.result)
        /////////            
        
        }   
    
}

API响应:

{
    "success": true,
    "message": "Advertisement Posted Successfully!"
}

模型类:

import Foundation

// MARK: - CommonData
struct CommonData: Codable {
    let success: Bool
    let message: String
}

错误:

失败(Alamofire.AFError.responseSerializationFailed(原因:Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(错误:Swift.DecodingError.typeMismatch(Swift.String,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“消息”,intValue: nil)],debugDescription:“预期解码字符串,但找到了字典。”,underlyingError:nil)))))

尝试发布 API 调用。

ios swift alamofire jsondecoder
1个回答
0
投票

当您成功

POST
时,响应将是
CommonData
,正如您所拥有的那样。

但是,当出现一些错误时,您会得到

CommonData2
(见下文)。

例如,在您的

params
中,需要
"location": ""
。 与
"max_transaction_limit": "2"
类似,注意它 > 1。 在这些情况下,您必须解码
CommonData2

当你使用

let params = ["email":"[email protected]","password":"Test@123"]
时,你会得到
CommonData2
回应。

 struct CommonData2: Codable {
     let success: Bool
     let result: String?
     let message: Message
 }

 struct Message: Codable {
     let errors: [String: [String]]
 }

要解码这两种类型的响应,您可以尝试这种方法,使用

init(from decoder: Decoder)
, 对我有用:

 struct CommonData2: Codable {
     let success: Bool
     let result: String?
     let message: Message?
     var msg: String?
     
     enum CodingKeys: String, CodingKey {
         case success, message, result
     }
     
     init(from decoder: Decoder) throws {
         let container = try decoder.container(keyedBy: CodingKeys.self)
         self.result = try? container.decode(String.self, forKey: .result)
         self.success = try container.decode(Bool.self, forKey: .success)
         if let theMessage = try? container.decode(Message.self, forKey: .message) {
             self.message = theMessage
         } else {
             self.message = nil
         }
         if let theString = try? container.decode(String.self, forKey: .message) {
             self.msg = theString
         } else {
             self.msg = nil
         }
     }

 }
 
© www.soinside.com 2019 - 2024. All rights reserved.