ios - AFnetworking:错误代码 500:无法读取数据,因为它的格式不正确
问题描述
在我的应用程序中,我使用 AFNetworking 进行 API 调用,并且它运行良好。但是对于一个 API,当我调用该 API 时,它会进入失败块并给我错误,错误代码 500:无法读取数据,因为它的格式不正确。
注意:当我在我的 json 中添加数组时,它只会给出错误。当我在 Postman 中传递该数据时,它会给我适当的响应。
这是我的代码:
func callAPIUsingPOSTAddActivity(filePath: String, params: NSMutableDictionary?, enableInteraction: Bool, showLoader: Bool, viewObj: UIView?, onSuccess: @escaping (AFAPICallerSuccess), onFailure: @escaping (AFAPICallerFailure)) {
let strPath = Global.baseURLPath + filePath;
print("URL = \(strPath) \n Param: \(String(describing: params))")
var viewSpinner: UIView!
if showLoader{
viewSpinner = self.addShowLoaderInView(viewObj: viewObj!, boolShow: showLoader, enableInteraction: enableInteraction)!
}
let afManager = AFHTTPSessionManager(sessionConfiguration: URLSessionConfiguration.default)
let authValue = Global.kLoggedInUserData().AccessToken
if authValue != nil || authValue != ""
{
afManager.requestSerializer.setValue(authValue, forHTTPHeaderField: "Authorization")
}
let headerSet = NSSet(objects: "application/json")
afManager.responseSerializer.acceptableContentTypes = headerSet as! Set<String>
var jsonString : String = ""
if (params != nil)
{
let jsonData = try? JSONSerialization.data(withJSONObject: params, options: [])
jsonString = String(data: jsonData!, encoding: .utf8)!
}
else
{
jsonString = ""
}
afManager.post(strPath, parameters: jsonString, progress: nil, success: { (task: URLSessionDataTask, responseObject: Any?) in
if showLoader{
self.hideRemoveLoaderFromView(removableView: viewSpinner, mainView: viewObj!)
}
let dictResponse = responseObject as! NSDictionary
// print(dictResponse)
if (dictResponse.object(forKey: "flag") as! Bool == true) || (dictResponse.object(forKey: "flag") as? Int == 1) { //no error
onSuccess(dictResponse, true)
}
else { //with error
if (showLoader) {
if ((dictResponse.object(forKey: "flag") as? String) != nil){
print(dictResponse)
Global.singleton.showWarningAlert(withMsg: dictResponse.object(forKey: "response") as! String)
}
}
onSuccess(dictResponse, false)
}
}) { (task: URLSessionDataTask?, error: Error) in
onFailure()
if let response = task?.response as? HTTPURLResponse {
print(response.statusCode)
if response.statusCode == 401 {
AFAPICaller().callAPIUsingTOKEN_Refresh_POST(enableInteraction: true, showLoader: false, viewObj: nil, onSuccess: { (_, _) in
}, onFailure: {
})
}
}
if (showLoader) {
print(error.localizedDescription)
Global.singleton.showWarningAlert(withMsg: LocalizeHelper().localizedString(forKey: "keyInternetMsg"))
self.hideRemoveLoaderFromView(removableView: viewSpinner, mainView: viewObj!)
}
}
}
这是我传递的json字符串
{"is_all_day":false,"account_type":"3","activity_type_id":"1","conference_call_pin":"","event_description":"","people_involved":[{"last_name":"","org_logo_url":{"big":"","small":"app\/assets\/images\/no_user.png"},"id":"12","country":"","question_id":"","ignore_contact":false,"contact_pic":"","option_id":"","address_line_2":"","contact_pic_url":{"big":"","small":"app\/assets\/images\/no_user.png"},"state":"","organization_logo":"","work_country":"","work_address_line_2":"","key":"new","address_line_1":"","fname":"kashish","work_city":"","email":"","is_support_user":false,"work_state":"","link_to_organization":false,"is_associate":false,"work_full_address":"","org_id":"","zip_code":"","is_custom":false,"city":"","work_zip_code":"","first_name":"kashish ","org_email":"","contact_id":"12","work_is_custom":"","select_to_invite":false,"full_address":"","company_name":"Tata Consultancy Services","organization_name":"","work_address_line_1":""}],"duration":"01:00","location_type":"","end_date":"05\/29\/2018","is_custom":false,"organization_involved":[],"assign_to":"1","is_duration":true,"calendar_master_id":"","is_repeat":false,"event_title":"Call","purpose_type_trans_id":"","conference_description":"","conference_call_type_id":"","purpose_description":"","event_location":"","start_date":"05\/29\/2018","start_time":"11:00","conference_custom_type":"","is_address":false}
当我在邮递员中传递它时,它的工作正常。我从最近两天开始尝试。如果您有任何解决方案,请帮助我。
解决方案
推荐阅读
- php - 如何编辑“尾巴”的输出?
- adfs - 如何根据 AD 组发出声明(布尔值)
- c# - 识别 DateTime 间隔接近度,并考虑以前的间隔
- javascript - 对于来自 Vue.js 和 Vuetify.js 中的函数的表中的数据
- java - 如果我在 Kotlin 中编程,为什么这是一个 Java 文件
- php - 尝试保存到数据库时出错调用未定义的方法 stdClass::save()
- android - 如何将 alpah 设置为
在xml中? - excel - 如何在 Excel Javascript API 中获取所选范围的数据?
- java - 没有找到合适的除法方法
- javascript - Fabric.js 未从 SVG 加载路径元素