在/ todos上解析来自POST的响应时出错

时间:2016-08-03 10:51:52

标签: ios json swift

我无法解析此方法的json数据。我收到错误"在/ todos"解析来自POST的响应时出错在方法中打印。有人请指导我用POST方法发布正确的数据并得到正确的结果吗?

func callAddWithPOST(Name mname:String, PhoneNo mphone:String, Email memail:String, Comment mcomments:String)
{

    let d_mname = mname
    let d_phoneno = mphone
    let d_email = memail
    let d_Comment = mcomments

    let Urlpost: String! = "http://smartrestaurants.com/RestaurantAPI.asmx/addTestimonial"

    let urlforpost = NSURL(string: Urlpost)
    let todosUrlRequest = NSMutableURLRequest(URL: urlforpost!)
    todosUrlRequest.HTTPMethod = "POST"

    let newTodo = ["reviewId": 111, "restaurantId": 93, "customerId": 143, "locationId": 393, "name": d_mname, "phone": d_phoneno, "email": d_email, "comment": d_Comment, "creationdate": "1/1/1900 12:00:00 AM"]

    let jsonTodo: NSData
    do {
        jsonTodo = try NSJSONSerialization.dataWithJSONObject(newTodo, options: [])
        todosUrlRequest.HTTPBody = jsonTodo
    } catch {
        print("Error: cannot create JSON from todo")
        return
    }
    todosUrlRequest.HTTPBody = jsonTodo

    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)

    let task = session.dataTaskWithRequest(todosUrlRequest) {
        (data, response, error) in
        guard let responseData = data else {
            print("Error: did not receive data")
            return
        }
        guard error == nil else {
            print("error calling POST on /todos/1")
            print(error)
            return
        }
        do {
            guard let receivedTodo = try NSJSONSerialization.JSONObjectWithData(responseData, options: []) as? [String: AnyObject] else
            {
                print("Could not get JSON from responseData as dictionary")
                return
            }
            print("The todo is: " + receivedTodo.description)

            guard let todoID = receivedTodo["name"] as? String else {
                print("Could not get todoID as int from JSON")
                return
            }
            print("The ID is: \(todoID)")
        }
        catch
        {
            print("error parsing response from POST on /todos")
            return
        }
    }
    task.resume()
}

0 个答案:

没有答案