Inside dataTaskWithRequest is not Executing. In Swift While Hitting the API

时光怂恿深爱的人放手 提交于 2019-12-01 14:53:38

Posting code working on my side based on our discussion:

//MARK: URLRequest

func sendData (){

    var dict = [String:AnyObject]()

    dict =
    [
        "data" : [
                [
                    "answer" : [9353091],
                    "question" : 31675931
                ],
                [
                    "answer" : [9353101],
                    "question"  : 31675936
                ]
        ],
        "end" : 5565665,
        "quizId" : 1206500,
        "start" : 5565656
    ]

    print(dict)

    let url = NSURL(string: "http://www.proprofs.com/quiz-school/mobileData/request.php?request=QuizTotal&module=PQ")!
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.HTTPBody = try!  NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions())
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    let task =  NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
        if(error != nil){
            print("ERRORRRRRRR : \(error)")
            return
        }

        print("RESPONSEEEEE : \(response)")

        do {
            let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]

            print("DIIIIIIIC \(dic)")
        }catch {
            print("adsfdasfdsfdsafdsfadsfs")
        }

        ((response as! NSHTTPURLResponse).statusCode)
    }
    task.resume()
}

Final Edit:

The OP did not had issue in the above mentioned code. After hours of discussion and chat with him I saw that the error was in the didSelectRowAtIndexPath, the project was crashing immediately after the sendData: method was called, hence not response was received from the server.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!