【发布时间】:2019-01-03 02:58:56
【问题描述】:
我有参数:
{"name": "oaseme dabo", "spouseName": "", "fatherName": "hajime kasuga", "income": "Please Choose", "homeAddress": "", "locationAccessibility": "Please Choose", "gender": "Please Choose", "exactOccupation": "", "maritalStatus": "Please Choose", "source_id": "123456", "dateOfBirth": "01 January 1900", "exactIncome": "1", "howInfluenceThisPerson": "Please Choose", "motherName": "shizuka kasuga", "knowThisPerson": "Please Choose", "prospectingStatus": "Please Choose", "ageBand": "Choose Age Band", "occupation": "Please Choose", "sourceOfProspect": "Family & Relatives", "agentCode": "123456", "totalChildren": "0", "howCloseAreYou": "Please Choose", "workAddress": "", "exactSourceOfProspect": ""}
我把它放进身体里:
class func sentContactBody(agentCode: String, source_id: String, dateOfBirth: String, ageBand: String, exactIncome: String, exactOccupation: String, fatherName: String, gender: String, homeAddress: String, howCloseAreYou: String, howInfluenceThisPerson: String, income: String, knowThisPerson: String, locationAccessibility: String, maritalStatus: String, motherName: String, name: String, occupation: String, prospectingStatus: String, sourceOfProspect: String, spouseName: String, totalChildren: String, workAddress: String, exactSourceOfProspect: String) -> Parameters{
let sentContact : Parameters = [
"agentCode": agentCode,
"source_id": source_id,
"dateOfBirth": dateOfBirth,
"ageBand": ageBand,
"exactIncome": exactIncome,
"exactOccupation": exactOccupation,
"fatherName": fatherName,
"gender": gender,
"homeAddress": homeAddress,
"howCloseAreYou": howCloseAreYou,
"howInfluenceThisPerson": howInfluenceThisPerson,
"income": income,
"knowThisPerson": knowThisPerson,
"locationAccessibility": locationAccessibility,
"maritalStatus": maritalStatus,
"motherName": motherName,
"name": name,
"occupation": occupation,
"prospectingStatus": prospectingStatus,
"sourceOfProspect": sourceOfProspect,
"spouseName": spouseName,
"totalChildren": totalChildren,
"workAddress": workAddress,
"exactSourceOfProspect": exactSourceOfProspect
]
return sentContact
}
我把它称为参数:
let param = HTTPBody.sentContactBody(agentCode: agentCode, source_id: source_id, dateOfBirth: dateOfBirth, ageBand: ageBand, exactIncome: exactIncome, exactOccupation: exactOccupation, fatherName: fatherName, gender: gender, homeAddress: homeAddress, howCloseAreYou: howCloseAreYou, howInfluenceThisPerson: howInfluenceThisPerson, income: income, knowThisPerson: knowThisPerson, locationAccessibility: locationAccessibility, maritalStatus: maritalStatus, motherName: motherName, name: name, occupation: occupation, prospectingStatus: prospectingStatus, sourceOfProspect: sourceOfProspect, spouseName: spouseName, totalChildren: totalChildren, workAddress: workAddress, exactSourceOfProspect: exactSourceOfProspect)
当我使用 Alamofire 调用时,它总是返回 nil,所以它被视为失败:
Alamofire.request(Constant.sentContactSAMDB(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersSAMDB())
.responseObject{ (response: DataResponse<SAMDBModel>) in
switch(response.result) {
case .success(_):
print("contact response result value:\(response.result.value)")
if response.result.value != nil {
self.samDBModel = response.result.value
let status = self.samDBModel?.status
let statusCode = response.response?.statusCode
print("sent contact statusCode:\(String(describing: statusCode))")
if(statusCode == 200){
let result = self.samDBModel?.result
let samDBStatus = result![0].status
let samDBError = result![0].error
print("sent contact samDBStatus:\(samDBStatus)")
}
}
break
case .failure(_):
print("contact failure response result value:\(response.result.value)")
self.samDBModel = response.result.value
let status = self.samDBModel?.status ?? "Failed connect to Tokio Marine server"
break
}
}
标头只有 Content-Type : application/json
使它工作的正确代码是什么?我已经放了 plist 异常域并允许任意加载 YES。
【问题讨论】:
-
可以尝试编码:URLEncoding.default
-
@ZahirulIslam , URLEncoding.default 也得到了联系失败响应结果值:nil
-
这是您第一次使用 Alamofire 吗?不要将其反序列化为 DataResponse
尝试使用 DataResponse 。或代替 . responseObject 使用 .responseJSON 然后手动转换 response.result。我相信这是反序列化部分的问题 -
@AndrewJamesRamirez,我尝试使用 .responseJSON{ (response) 也得到了联系失败响应结果值:nil