AFnetworking:错误代码500:无法读取数据,因为它的格式不正确

时间:2018-05-29 05:51:05

标签: ios swift alamofire afnetworking

在我的应用程序中,我正在使用AFNetworking进行API调用,并且其工作正常。但是对于一个API,当我调用该API时,它会进入Failure块并给我错误,错误代码500:无法读取数据,因为它的格式不正确。

注意:当我在json中添加数组时,它只会出错。当我在邮递员中传递这些数据时,它给了我适当的回应。

这是我的代码:

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}

当我在邮递员中传递它的工作正常。 我是从最近两天开始尝试的。如果你有任何解决方案,请帮助我。

enter image description here

0 个答案:

没有答案