ios Alamofire故障-响应序列化失败

irlmq6kh  于 2023-03-24  发布在  iOS
关注(0)|答案(1)|浏览(373)
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":"smith@mailinator.com","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回复:
{“成功”:真,“消息”:“广告发布成功!”}
型号分类:

import Foundation

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

'
错误:
failure(Alamofire.AFError.responseSerializationFailed(reason:Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(错误:Swift.解码错误.类型不匹配(Swift.字符串,Swift.解码错误.上下文(编码路径:[编码键(字符串值:“message”,intValue:nil)],调试说明:“应解码字符串,但找到的却是字典。",underlyingError:无))))))
尝试发布API调用

yacmzcpb

yacmzcpb1#

当你有一个成功的POST时,响应是CommonData
但是,当出现一些错误时,则会得到CommonData2(见下文)。
例如,在params中,需要"location": ""。与"max_transaction_limit": "2"类似,注意它〉1。在这些情况下,您必须解码CommonData2
当你使用let params = ["email":"smith@mailinator.com","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
         }
     }

 }

相关问题