2016-06-15 123 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