why is this error popping out
***MC] Reading from public effective user settings.
Error: Couldn't decode data into Blog: dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Garbage*i have this JSON*
{"Response":"false","Info":[{"Estatus":"Sin Conexion"}]}this is the structure of my json in swift
struct Blog: Decodable {
let Response: String
let articles: [Article]
enum CodingKeys : String, CodingKey {
case Response
case articles = "Info"
}
}
struct Article: Decodable {
let Estatus: String
}and this is all my code
@IBActionfunc enviarDestinoAccion1(_ sender: UIButton) {
let id1 = UserDefaults.standard.string(forKey: "id1conductor")
let id1pa = UserDefaults.standard.string(forKey: "idUser")
if adondevaxd.text == "" || adondevaxd.text == " " || adondevaxd.text == "123" || adondevaxd.text == "abc" || adondevaxd.text == "."
{
self.displayAlert(title: "Usuario", message: "Debe elegir un destino")
}
let myURL = URL(string: "hehe")
var request = URLRequest(url: myURL!)
request.httpMethod = "POST"
let posString = "ID_Conductor=\(id1!)&Destino=\(adondevaxd.text!)&ID_Pasajero=\(id1pa!)"
request.httpBody = posString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
if let error = error {
print("error=\(error)")
return
}
guard let data = data else {
print("Something wrong")
return
}
struct Blog: Decodable {
let Response: String
let articles: [Article]
enum CodingKeys : String, CodingKey {
case Response
case articles = "Info"
}
}
struct Article: Decodable {
let Estatus: String
}
// Pendiente ***QUE YA tengo una solicitud pendiente
// Ocupado **que alguien mas lo ganó
// Solicitado ** que se envio tu solicitud al conductor empezar un tiempo de cada minuto para saber si el conductor ya respondió
do {
let blog = try JSONDecoder().decode(Blog.self, from: data)
DispatchQueue.main.async {
if blog.Response == "true" {
//para ver como actua el json
// print(blog)
for article in blog.articles {
let tipoResponse = article.Estatus
if tipoResponse == "Solicitado"
{
self.displayAlert(title: "Usuario", message: "El conductor será notificado, espera la respuesta")
self.buttonOulet.isHidden = true
self.adondevaxd.isHidden = true
self.adondevaxd.text = ""
self.adondequiereirxd.isHidden = true
self.enviarDestino4.isHidden = true
self.enviarDestino3.isHidden = true
self.enviarDestino2.isHidden = true
self.enviarDestino1.isHidden = true
self.credencial1.isHidden = true
self.Credencial2.isHidden = true
self.Credencial3.isHidden = true
self.Credencial4.isHidden = true
}
else if tipoResponse == "Ocupado" {
self.displayAlert(title: "Usuario", message: "El conductor ya se encuentra ocupado, selecciona otro")
self.map.isHidden = false
self.buttonOulet.isHidden = false
self.adondevaxd.isHidden = true
self.adondevaxd.text = ""
self.adondequiereirxd.isHidden = true
self.enviarDestino4.isHidden = true
self.enviarDestino3.isHidden = true
self.enviarDestino2.isHidden = true
self.enviarDestino1.isHidden = true
self.credencial1.isHidden = true
self.Credencial2.isHidden = true
self.Credencial3.isHidden = true
self.Credencial4.isHidden = true
}
else if tipoResponse == "Solicitado" {
self.displayAlert(title: "Usuario", message: "Se envio tu solicitud al conductor, porfavor ten paciencia")
self.adondequiereirxd.isHidden = true
self.adondevaxd.isHidden = true
self.adondequiereirxd.isHidden = true
}
break
}
}
else if blog.Response == "false" {
self.displayAlert(title: "Usuario", message: "No hay conexion")
self.map.isHidden = false
self.buttonOulet.isHidden = false
self.adondevaxd.isHidden = true
self.adondequiereirxd.isHidden = true
self.adondevaxd.text = ""
self.enviarDestino4.isHidden = true
self.enviarDestino3.isHidden = true
self.enviarDestino2.isHidden = true
self.enviarDestino1.isHidden = true
self.credencial1.isHidden = true
self.Credencial2.isHidden = true
self.Credencial3.isHidden = true
self.Credencial4.isHidden = true
}
}
}
catch {
print("Error: Couldn't decode data into Blog:", error)
return
}
}
task.resume()
}is my PHP wrong?
any hints? :S
require_once '../data/d.php';
error_reporting(0);
if(isset($_POST['ID_Pasajero'])){
$Comparacion = "SELECT * FROM Solicitudes where ID_Conductor = '".$_POST['ID_Conductor']."' and Estado != '2'";
$R = $conexion->query($Comparacion);
$Count = mysqli_num_rows($R);
if ($Count > 0) {
$Comparacion = "SELECT * FROM Solicitudes where ID_Pasajero = '".$_POST['ID_Pasajero']."' and Estado != '2'";
$R = $conexion->query($Comparacion);
$Count = mysqli_num_rows($R);
if ($Count > 0) {
$Resultado = '{"Response":"true","Info":[{"Estatus":"Pendiente"}]}';
}else{
$Resultado = '{"Response":"true","Info":[{"Estatus":"Ocupado"}]}';
}
}else{
$Comparacion = "SELECT * FROM Solicitudes where ID_Pasajero = '".$_POST['ID_Pasajero']."' and Estado != '2'";
$R = $conexion->query($Comparacion);
$Count = mysqli_num_rows($R);
if ($Count > 0) {
$Resultado = '{"Response":"true","Info":[{"Estatus":"Pendiente"}]}';
}else{
$sql = "INSERT INTO Solicitudes(ID_Pasajero, ID_Conductor, Destino, Estado, Estado_Calificacion) VALUES ('$_POST[ID_Pasajero]', '$_POST[ID_Conductor]', '$_POST[Destino]', '0', '0')";
$Resultado = '{"Response":"true","Info":[{"Estatus":"Solicitado"}]}';
}
}
}
if ($conexion->query($Comparacion) === TRUE) {
if ($conexion->query($sql) === TRUE) {
echo $Resultado;
} else {
echo $Resultado;
}
} else {
if ($conexion->query($sql) === TRUE) {
echo $Resultado;
}
echo '{"Response":"false","Info":[{"Estatus":"Sin Conexion"}]}';
}
$conexion->close();
?>
is my PHP wrong?
As I wrote before, this is not a place to discuss PHP programming, but your PHP code is wrong enough and far from production quality.
- Never use `error_reporting(0);` You should make some effort to write a code which generates no messages with default `error_reporting` setting.
- Never embed POST values directly in your SQL, which causes SQL Injection vulnerability. Use prepared statement.
(And/or properly sanitize all POST values or escape them.)
- You should better not mix up Object-orienented notation like `->query` and procedural notation like `mysqli_num_rows`.
- `->query` (assuming its `mysqli_query`) never teturns TRUE for SELECT statement. Your if-condition `$conexion->query($Comparacion) === TRUE` has no meaning. (And you should not execute the same query again.)
- Your broken indentation makes your code hard to read, but it is unclear in which case what you want to do and in which case what result you want to return. Especially, when `$conexion->query($sql) === TRUE` (though it's not guaranteed whether `$sql` is initialized), your PHP code returns both `$Resultado` and `'{"Response":"false",...}`, which is not valid JSON.