内部dataTaskWithRequest不是Executing。在Swift中击中API

时间:2016-08-31 06:13:02

标签: ios swift nsurlsession

当我使用POSTMAN进行同样的数据时,我得到了所需的响应。但是在我的函数里面 dataTaskWithRequest 我没有得到任何响应。

func sendData (){
            let url = NSURL(string: "http://www.example.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(error)
                    return
                }
                ((response as! NSHTTPURLResponse).statusCode)
            }
            task.resume()
}

关于数据任务

My Code Break Point 在数据任务之后

After DataTaskWithRequest 邮递员回应 谢谢任何帮助将不胜感激。

我要发送的字典

{
    data =     (
                {
            answer =             (
                9353091
            );
            question = 31675931;
        },
                {
            answer =             (
                9353101
            );
            question = 31675936;
        },
                {
            answer =             (
                9353111
            );
            question = 31675941;
        },
                {
            answer =             (
                9353121
            );
            question = 31675946;
        },
                {
            answer =             (
                9353131
            );
            question = 31675951;
        },
                {
            answer =             (
                9353141
            );
            question = 31675954;
        },
                {
            answer =             (
                9353151
            );
            question = 31675961;
        },
                {
            answer =             (
                9353279
            );
            question = 31676023;
        },
                {
            answer =             (
                9353289
            );
            question = 31676026;
        },
                {
            answer =             (
                9353299
            );
            question = 31676031;
        }
    );
    end = 5565665;
    quizId = 1206500;
    start = 5565656;
}

1 个答案:

答案 0 :(得分:0)

根据我们的讨论发布代码在我身边工作:

//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()
}

最终修改:

OP在上述代码中没有问题。经过几个小时的讨论并与他聊天后,我发现错误发生在didSelectRowAtIndexPath,项目在调用sendData:方法后立即崩溃,因此未收到服务器的响应。