I can’t get information from my API via android app

Asked

Viewed 136 times

0

I created a web API with the code:

 <?php

    header('Content-type: Application/JSON');

    include("../Classes/conexao.php");

    if($_SESSION['conectado'] == false){
        echo json_encode("Sem resposta do servidor");
    }else{

        $usuario = $_REQUEST['usuario'];
        $senha = $_REQUEST['senha'];

        $consulta = "SELECT * FROM estabelecimento WHERE cpf_responsavel = '$usuario' AND senha = '$senha';";
        $resultado = $mysqli->query($consulta) or die($mysqli->error);

        $n = mysqli_num_rows($resultado);

        for ($i=0; $i < $n; $i++) { 
            $dados[] = $resultado->fetch_assoc();       
        }

        echo json_encode((object)$dados, JSON_PRETTY_PRINT);

    }

?> 

Which returns the following json:

 {
    "0": {
        "id": "",
        "cnpj": "",
        "cpf_responsavel": "",
        "url_logo": "http",
        "senha": "",
        "especialidade": "",
        "razao_social": "",
        "avaliacao": ""
    }
} 

I created tbm an android app to consume this API using retrofit. The problem is that the app can connect to the service only I can’t get it to save any of the data expressed in json.

2 answers

0

I managed to solve. I am using in android the retrofit2 library that requires a specific formatting of json. To get into such formatting I mounted the json object manually:

$json = [
            "Estabelecimento"  => [
                    "id" => "".$dados['id']."",
                    "cnpj" => "".$dados['cnpj']."",
                    "cpf_responsavel" => "".$dados['cpf_responsavel']."",
                    "url_logo" => "".$dados['url_logo']."",
                    "senha" => "".$dados['senha']."",
                    "especialidade" => "".$dados['especialidade']."",
                    "razao_social" => "".$dados['razao_social']."",
                    "avaliacao" => "".$dados['avaliacao'].""]
            ];


        echo json_encode($json, JSON_PRETTY_PRINT);

Got that exit:

{
"Estabelecimento": {
    "id": "700",
    "cnpj": "555",
    "cpf_responsavel": "799",
    "url_logo": "http",
    "senha": "123",
    "especialidade": "nada",
    "razao_social": "JV",
    "avaliacao": "5"
}

}

So the app was able to rescue all the data

0


I believe you are not being able to save because the array you are transforming into json is a multidimensional array. So it generates the indexes in your JSON that I believe is unnecessary.

Make the following step and see if the problem will be solved.

In your php code, instead of generating an associative array of query results, generate an object for each line.

for ($i=0; $i < $n; $i++) {
    $dados[] = $resultado->fetch_object();
}

 echo json_encode($dados, JSON_PRETTY_PRINT);

Note that I am not converting the whole array to object but each line returned from the query to the bank.

  • That was the output: [ { "id": " ", "cnpj": " ", "cpf_responsavel": " ", "url_logo": " ", "password": " ", "specialty": " ", "razao_social": " ", "rating": " " } ]

  • But still the app can not consume the data

Browser other questions tagged

You are not signed in. Login or sign up in order to post.