Search code examples
jsonswiftnsdata

response Data is nil


I am getting response data nil.

func fetchSinglePageData() {
    var response: NSData?
    var errors: NSError?
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

        response = NSData(contentsOfFile:"url...?project_id=(self.projectID)&project_page=Request", options: NSDataReadingOptions(0), error: &errors)

        print("LatestURL Single page:(response)")

        if response == nil {
            var alertview = UIAlertView(title: "Network Error", message: "Data not received due to network connection.Try again...", delegate: self, cancelButtonTitle: "Ok")
            alertview.show()
        }
        else{
            let jsonDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(response!, options: nil, error: &errors) as NSDictionary
            print("JSON Response Single page:(jsonDict)")
            var statuses = [NSObject : AnyObject]()
            self.lblstartTime?.text = jsonDict["start_time"] as? String
            self.lblcurrentStage?.text = jsonDict["current_stage"] as? String
            self.lblcompletionTime?.text = jsonDict["completion_time"] as? String
            self.lblManager?.text = jsonDict["manager"] as? String
        }
    })
}    

project_id is string which is recevied from presvious page which is correctly working.


Solution

  • func singlePageData(){
    
        var errors: NSError?
    
        let urlString = "xyz.com"
        print("URL RESPONSE  \(urlString)")
    
        let request = NSURLRequest(URL: NSURL(string: urlString), cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 50)
    
        let session = NSURLSession.sharedSession()
    
        let task = session.dataTaskWithRequest(request) { (data, response, error) in
    
            if error != nil{
                //handel error
                print(error!.localizedDescription)
                var alertview = UIAlertView(title: "Network Error", message: "Data not received due to network connection.Try again...", delegate: self, cancelButtonTitle: "Ok")
                alertview.show()
                return
            }
    
            if let responseData = data{
    
                  var jsonDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableContainers, error: &errors) as NSDictionary!
    
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
    
                println("Json dict value  \(jsonDict)")
                self.lblprojectName?.text = jsonDict["name"] as? String
                println("lblprojectName:  \(self.lblprojectName?.text)")
                self.lblstartTime?.text = jsonDict["start_time"] as? String
                 println("lblstartTime :  \(self.lblstartTime?.text)")
                self.lblcurrentStage?.text = jsonDict["current_stage"] as? String
                 println("lblcurrentStage :  \(self.lblcurrentStage?.text)")
                self.lblcompletionTime?.text = jsonDict["completion_time"] as? String
                 println("lblcompletionTime :  \(self.lblcompletionTime?.text)")
                self.lblManager?.text = jsonDict["manager"] as? String
    
                })
    
                }
            }
        task.resume()
    
    }
    

    This is my answer as per Jitendra Solanki answer i have made changes to code and it is now working in Swift 1.2