// // EncuestaViewController.swift // Encuesta_MARLE // // Created by Tatiana Castro on 1/31/19. // Copyright © 2019 Tatiana Castro. All rights reserved. // import UIKit import ResearchKit //let semaphore = DispatchSemaphore(value: -1) var token = String() let URL = "http://emaapp.online/getSubQ2.php?tk="+token; var preguntas = [String]() var descripcion = String() var id_preg = [String]() var tipo_preg = [String]() var cantidad_preguntas = Int() var max_values = [String]() var max_text = [String]() var min_text = [String]() var id_questionario = String() var jsonString = String() var jsonDict = [String: Any]() var n_jsonDict = [String: Any]() var response_survey = String() func getJsonFromUrl(){ print("entre a getJsonFromUrl") let request = NSMutableURLRequest(url: NSURL(string: "http://emaapp.online/getSubQ2.php?tk="+token)! as URL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 60) // let request = NSMutableURLRequest(url: NSURL(string: "http://emaapp.online/getSubQ2.php?tk="+token)! as URL) request.httpMethod = "POST" let postString = "data=\(jsonString_reg)" request.httpBody = postString.data(using: String.Encoding.utf8) let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in if error != nil { print("error=\(String(describing: error))") return } let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary { if let id = jsonObj!.value(forKey: "id_subquestionnair") as? String { id_questionario = id } if let des = jsonObj!.value(forKey: "description") as? String { descripcion = des } if let cantidad = jsonObj!.value(forKey: "cantidad_preguntas") as? String { cantidad_preguntas = Int(cantidad) ?? 0 } if let jsonArray = jsonObj!.value(forKey: "preguntas") as? NSArray { for subj in jsonArray{ if let subjDict = subj as? NSDictionary { if let pregunta = subjDict.value(forKey: "premise") { preguntas.append((pregunta as? String)!) } } if let subjDict = subj as? NSDictionary { if let id = subjDict.value(forKey: "id_question") { id_preg.append((id as? String)!) } } if let subjDict = subj as? NSDictionary { if let tipo = subjDict.value(forKey: "id_type") { tipo_preg.append((tipo as? String)!) } } if let subjDict = subj as? NSDictionary { if let val = subjDict.value(forKey: "max_val") { max_values.append((val as? String)!) } } if let subjDict = subj as? NSDictionary { if let mx_txt = subjDict.value(forKey: "max_text") { max_text.append((mx_txt as? String)!) } } if let subjDict = subj as? NSDictionary { if let mn_txt = subjDict.value(forKey: "min_text") { min_text.append((mn_txt as? String)!) } } } } } print("response solito = \(String(describing: response))") //Error: 1062 print("responseString reg= \(String(describing: responseString))") //if(responseString) response_survey = responseString! as String } task.resume() // let url = NSURL(string: URL) // URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in // if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary { // print("entre a jsonObj") // print(jsonObj as Any) // if let id = jsonObj!.value(forKey: "id_subquestionnair") as? String { // id_questionario = id // } // if let des = jsonObj!.value(forKey: "description") as? String { // descripcion = des // } // if let cantidad = jsonObj!.value(forKey: "cantidad_preguntas") as? String { // cantidad_preguntas = Int(cantidad) ?? 0 // } // if let jsonArray = jsonObj!.value(forKey: "preguntas") as? NSArray { // for subj in jsonArray{ // if let subjDict = subj as? NSDictionary { // if let pregunta = subjDict.value(forKey: "premise") { // preguntas.append((pregunta as? String)!) // } // } // if let subjDict = subj as? NSDictionary { // if let id = subjDict.value(forKey: "id_question") { // id_preg.append((id as? String)!) // } // } // if let subjDict = subj as? NSDictionary { // if let tipo = subjDict.value(forKey: "id_type") { // tipo_preg.append((tipo as? String)!) // } // } // if let subjDict = subj as? NSDictionary { // if let val = subjDict.value(forKey: "max_val") { // max_values.append((val as? String)!) // } // } // if let subjDict = subj as? NSDictionary { // if let mx_txt = subjDict.value(forKey: "max_text") { // max_text.append((mx_txt as? String)!) // } // } // if let subjDict = subj as? NSDictionary { // if let mn_txt = subjDict.value(forKey: "min_text") { // min_text.append((mn_txt as? String)!) // } // } // } // } // if error != nil { // print("error=\(String(describing: error))") // return // } // print("response = \(String(describing: response))") // let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) // print("responseString = \(String(describing: responseString))") // response_survey = responseString! as String // } // }).resume() // semaphore.signal() //semaphore.wait(semaphore,) // dispatch_semaphore_wait(semaphore,dispatch_time_t(DispatchTime.distantFuture)) } func sendJson(){ let request = NSMutableURLRequest(url: NSURL(string: "http://emaapp.online/parseAnswers2.php")! as URL) request.httpMethod = "POST" let postString = "data=\(jsonString)" request.httpBody = postString.data(using: String.Encoding.utf8) let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in if error != nil { print("error=\(String(describing: error))") return } print("response = \(String(describing: response))") let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) print("responseString = \(String(describing: responseString))") } task.resume() } func convertToDictionary(text: String) -> [String: Any]? { if let data = text.data(using: .utf8) { do { return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] } catch { print(error.localizedDescription) } } return nil } func limpiarJson()->Dictionary{ var data = [String:Any]() n_jsonDict["token"]=String(token) n_jsonDict["os"]="ios" let resultados_encuestas = jsonDict["results"] as! [[String:Any]] for i in 0..