2016-10-22 37 views
0

我已經做了一個API調用添加產品到購物車。而在按鍵的動作我這樣做API calling.I需要發送一些特定的參數,當我使用郵遞員代碼。其在我的代碼引發許多錯誤API。但是:後迅速發送的API調用參數

func addtocartapicalling() 
{ 


    let headers = [ 
     "cache-control": "no-cache", 
     "postman-token": "4c933910-0da0-b199-257b-28fb0b5a89ec" 
    ] 

    let postData = NSData(data: "{ 
     "cartType" : "1", 
     "cartDetails" : { 
     "customerID" : "u", 
     "cartAmount" : "6999", 
     "cartShipping" : "1", 
     "cartTax1" : "69", 
     "cartTax2" : "", 
     "cartTax3" : "", 
     "cartCouponCode" : "", 
     "cartCouponAmount" : "", 
     "cartPaymentMethod" : "", 
     "cartProductItems" : { 
     "productID" : "9", 
     "productPrice" : "6999", 
     "productQuantity" : "1" 
     } 
    } 
}".dataUsingEncoding(NSUTF8StringEncoding)!) 

var request = NSMutableURLRequest(URL: NSURL(string: "http://api.php")!, 
            cachePolicy: .UseProtocolCachePolicy, 
            timeoutInterval: 10.0) 
request.HTTPMethod = "POST" 
request.allHTTPHeaderFields = headers 
request.HTTPBody = postData 

let session = NSURLSession.sharedSession() 
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in 
    if (error != nil) { 
     println(error) 
    } else { 
     let httpResponse = response as? NSHTTPURLResponse 
     println(httpResponse) 
    } 
}) 

dataTask.resume() 
} 

enter image description here

見我上面的代碼,是否有任何API調用,請幫助我。

感謝

let string = "{\"cartType" : "1" + "cartDetails" : { "customerID" : "u", + "cartAmount" : "6999", + "cartShipping" : "1", + "cartTax1" : "69", + "cartTax2" : "", + "cartTax3" : "", + "cartCouponCode" : "", + "cartCouponAmount" : "", + "cartPaymentMethod" : "",} + "cartProductItems" : { "productID" : "9", + "productPrice" : "6999", + "productQuantity" : "1" }" }" 

回答

1

你不能只是從別的地方複製代碼,並期望它神奇的工作。主要問題是你不能在Swift中將多個文本字符串分割。可以將它放在一行中,也可以通過多行添加多個文字字符串。例如...

let string = "{ \"cartProductItems\" : " + 
     "{\"productID\" : \"9\"," + 
     "\"productPrice\" : \"6999\"}}" 

let postData = NSData(data: string.data(using: .utf8)!) 

一旦固定這大部分的其他錯誤的,你應該能夠通過點擊錯誤本身的Xcode將修復項目爲你喜歡NSURLSession has been renamed URLSession

+0

我試着寫修復像你的解決方案。但仍然在該行的一些錯誤。就像要求我添加逗號,就像那 – mack

+0

這不足以幫助我的信息。你需要用你的新代碼更新你的答案。 – Frankie

+0

請檢查我的updat – mack