I am making a connection from an application to a web service, the web service requires two fields to be able to return a result, the problem is that I get an error when executing my code to connect to the web service, this is the error:
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
I have reviewed my code but I can not find the error, I have tried to change the method of sending "POST" to "GET" but it works the same, if I write the URL in the search engine and I pass the two parameters for the query works correctly
this is my URL local:
http://10.0.2.15/MGrex/listaVideos.php?nombreS=Enrique&telefonoS=5535976020
This is my IBAction that executes the query to the Web Service:
@IBAction func desplegarWebService(_ sender: Any) {
//Recuperamos los datos de los TextField
let nombre = nombreTextField?.text
let telefono = telefonoTextField?.text
if(nombre == "" || telefono == ""){
print("Debes ingresar el nombre y el telefono para consultar")
return
}
let post = "nombreS=\(nombre!)&telefonoS=\(telefono!)"
let url = URL(string: "http://10.0.2.15/MGrex/listaVideos.php")
var request = URLRequest(url: url!)
request.httpMethod = "GET"//Tipo de envio de informacion
request.httpBody = post.data(using: .utf8)//Pasamos las variables a la cabecera con codificacion UTF8
let task = URLSession.shared.dataTask(with: request){ data, response, error in guard let data = data else{//Si esxiste un error se termina la ejecucion
print("Error en el servidor")
return
}
do{//Creamos un objeto Json para los valores
print("Recibimos respuesta")
if let json = try JSONSerialization.jsonObject(with: data) as? [String: String]{
DispatchQueue.main.async {
//let id = json["id"]
let nombre = json["nombre"]
let direccion = json["direccion"]
let telefono = json["telefono"]
self.nombreTextField.text = nombre
self.direccionTextField.text = direccion
self.telefonoTextField.text = telefono
print(nombre!)
print(direccion!)
print(telefono!)
}
}
} catch let parseError{
print("Error al parsear: \(parseError)")
print("Error del servidor JSON")
let responseString = String(data: data, encoding: .utf8)
print("Respuesta: \(responseString!)")
}
}
task.resume()
}
and this is my Web Service on PHP:
<?PHP
$servername="localhost";
$database="MGrex";
$username="root";
$password="";
$json = array();
if(isset($_GET["nombreS"]) && isset($_GET["telefonoS"])){
$nombreS = $_GET["nombreS"];
$telefonoS = $_GET["telefonoS"];
$conexion = mysqli_connect($servername,$username,$password,$database);
mysqli_set_charset($conexion, "utf8");
$consulta = "select * from soldados WHERE nombre = '{$nombreS}' AND telefono = '{$telefonoS}' ORDER BY id DESC";
$resultado = mysqli_query($conexion, $consulta);
while($registro=mysqli_fetch_array($resultado)){
$json['video'][]=$registro;
}
}else{
$resultar["id"]='No registrado';
$resultar["nombre"]='No registrado';
$resultar["direccion"]='No registrado';
$resultar["telefono"]='No registrado';
$json['video'][]=$resultar;
}
mysqli_close($conexion);
echo json_encode($json);
?>
Aucun commentaire:
Enregistrer un commentaire