简体   繁体   English

如何使用Alamofire发送复杂类型请求

[英]How to send complex type request using Alamofire

I have to send request to Server. 我必须向服务器发送请求。 Saving user record. 保存用户记录。 The user record contains the model of Subject, and that Subject contains multiple model and arrays also. 用户记录包含Subject的模型,并且Subject也包含多个模型和数组。 let me give you little example. 让我给你一点例子。

@ignore 
public class SubjectModel: Codable {
    public var Name : String? = ""
    public var Price : Int? = 0
    public var ISBN : String? = ""
    public var listTeachers : [TeacherModel]? = []

}

public class TeacherModel: Codable {
    public var Name : String? = ""
    public var PhoneNo : String? = ""
    public var listClasses: [ClassModel]? = []

}

public class ClassModel: Codable {
    public var Name : String? = ""
    public var Section : String? = ""
     public var RoomNo : String? = ""


}

So I have to send the SubjectModel, and as you can see the SubjectModel contains multiple Arrays of InnerModel. 因此,我必须发送SubjectModel,并且您可以看到SubjectModel包含多个InnerModel数组。 All models are codable. 所有型号都是可编码的。

So the JSON that my webservice required must look like following 因此,我的Web服务所需的JSON必须如下所示

{ApiKey:"asas-dsadas-xyz-xyz","StudentSubjects":{Now here come arrays,"InnerList":[here comes inner arrays ]}} {ApiKey:“ asas-dsadas-xyz-xyz”,“ StudentSubjects”:{现在是数组,“ InnerList”:[这里是内部数组]}}

Problems: 问题:

  1. As you can see I have Implemented Codables to my model, so I am getting good and expected Json, But I am not able to attach the Api Key 如您所见,我已经在模型中实现了Codables,因此我得到了很好的期望Json,但是我无法附加Api密钥
  2. Also I have no Idea how to send such thing as Parameters of Alamofire. 我也不知道如何发送Alamofire参数之类的东西。
  3. Or You can say it simply like" How to send the StudentSubject and Apikey also in json format " 或者,您也可以简单地说,例如“ 如何也以json格式发送StudentSubject和Apikey

Please help me, I am bashing my head but not able to get the reply on server side, infact I am getting null. 请帮助我,我正在b头,但无法在服务器端得到答复,实际上我正在获得null。

UPDATE1: This is how I am doing it UPDATE1:这就是我的做法

let jsonEncoder = JSONEncoder()
    let jsonData = try jsonEncoder.encode(mySubjectModel)
    let json = String(data:jsonData,encoding:String.Encoding.utf8)


let parameter: [String:AnyObject] = [
            "StudentSubjects" : json as AnyObject,
            "ApiKey":"xyz-zyz-zyz"
    ]

And then Sending it like this 然后像这样发送

Alamofire.request(Common.URL+ServiceMethodName, method: .post, parameters:parameter, encoding: JSONEncoding.default, headers: nil) .responseJSON { response in

What you need is to use this method (I'm not sure if you need PUT or POST but it should be POST) 您需要使用此方法(我不确定您是否需要PUT或POST,但应为POST)

Alamofire.request(.POST, "http://myserver.com", parameters: parameters, encoding: .JSON)
    .responseJSON { request, response, JSON, error in
        print(response)
        print(JSON)
        print(error)
    }

parameters object is 参数对象是

let parameters: [String: AnyObject] = [
    "ApiKey" : "asas-dsadas-xyz-xyz",
    "StudentSubjects" : yourStudnetSubjectsList
]

Alamofire will do the transformation to JSON for you. Alamofire将为您完成向JSON的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM