2016-06-15 46 views
-2

我是swift新手,第一次使用Flipkart Affiliate API。我已經編寫代碼來獲取所有產品。請求中使用頭部。 貝婁是我正在嘗試的代碼。但沒有得到任何迴應。請幫忙 !! 讓我知道我是否發送正確的請求,以及如何得到回覆。使用Flipkart API無法獲取JSON響應

 super.viewDidLoad() 

     let urlString ="https://affi/affiliate/1.0/feeds/.." 

     let request = NSMutableURLRequest(URL: NSURL(string: urlString)!) 
     request.addValue("xxx", forHTTPHeaderField: "Fk-Affiliate-Id") 
     request.addValue("xxxxx", forHTTPHeaderField: "Fk-Affiliate-Token") 
     request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
     request.addValue("application/json", forHTTPHeaderField: "Accept") 
     let body: Dictionary <String,String>= Dictionary() 
     let session = NSURLSession.sharedSession() 
     request.HTTPMethod = "POST" 

     do { 
      request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(body ,options: []) 
     } catch { 
      print(error) 
      return 
     } 
     let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 
      // handle error 
      guard error == nil else { return } 
      print("Response: \(response)") 
      let strData = NSString(data: data!, encoding: NSUTF8StringEncoding) 
      print("Body: \(strData)") 

      let json: NSDictionary? 
      do { 
       json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary 
      } catch let dataError { 
       // Did the JSONObjectWithData constructor return an error? If so, log the error to the console 

       print(dataError) 
       let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) 
       print("Error could not parse JSON: '\(jsonStr)'") 
       // return or throw? 
       return 
      } 

     }) 

     task.resume() 
} 

回答

-1

嘗試:

do { 
json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary 
print(json) //Prints the received JSON if the request made is correct. 
} 
+0

那麼可能誰downvoted解釋他爲什麼這樣做,什麼是打印接收的JSON的正確方法,請人? – Dershowitz123