Send data from an api android using HttpURLConnection sending in json to a web service in php

0

I am here because I have a problem and I can not find a solution for it, I have already programmed the HttpManager class to send and receive data from my web service in php. Receiving the data I got, the problem is when sending data to the web service in php. Here is the android code to send the data:

    public static void setDados(String latitude, String longitude, String paciente, String uri)
{
    JSONObject envio = new JSONObject();
    try {
        envio.put("Latitude", latitude);
        envio.put("Longitude", longitude);
        envio.put("Paciente", paciente);
        URL url = new URL(uri);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        Uri.Builder builder = new Uri.Builder()
                .appendQueryParameter("Latitude", latitude)
                .appendQueryParameter("Longitude", longitude)
                .appendQueryParameter("Paciente", paciente);
        String query = builder.build().getEncodedQuery();

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));

        writer.write(envio.toString());
        Log.e("Conteudo: ",envio.toString());

        writer.flush();
        writer.close();
        os.close();
        conn.connect();

    }
    catch(Exception err)
    {
        Log.e("Conteudo: ", "Falha de conexão!!");
    }

}

The problem is that at the time of trying the connection sending the parameters the try of setDados is falling in catch and is giving connection failure:

Log.e("Conteudo: ", "Falha de conexão!!");

And here's the script in php to enter the data in the database:

include("BD.php");
$jsonImport = file_get_contents('php://input');
$resultado = json_decode($jsonImport,true);
$Latitude=$resultado['Latitude'];
$Longitude=$resultado['Longitude'];
$Paciente=$resultado['Paciente'];
$query = "INSERT INTO 'coordenadas'('Latitude', 'Longitude', 'Paciente') 
VALUES ('$Latitude','$Longitude','$Paciente')";
$inserido=$conexao->query($query);
if($inserido)
echo 'SUCESSO!';
else 
echo 'FRACASSO!';
$conexao->close();
    
asked by anonymous 19.07.2018 / 21:42

0 answers