How can I send a JSON object from my android to php?

1

I need to send a JSON object from my android app to my api in php. How can I do this?

public void btnCadastrar(View view) throws JSONException, IOException {

    final EditText edtNome = ((EditText) findViewById(R.id.edtNome));
    final EditText edtSobrenome = ((EditText) findViewById(R.id.edtSobrenome));
    EditText edtCelular = ((EditText) findViewById(R.id.edtCelular));
    EditText edtCep = ((EditText) findViewById(R.id.edtCep));
    EditText edtLogradouro = ((EditText) findViewById(R.id.edtLogradouro));
    EditText edtBairro = ((EditText) findViewById(R.id.edtBairro));
    EditText edtLocalidade = ((EditText) findViewById(R.id.edtCidade));
    EditText edtUF = ((EditText) findViewById(R.id.edtUF));

    final String nome = edtNome.getText().toString();
    String sobrenome = edtSobrenome.getText().toString();
    String celular = edtCelular.getText().toString();
    String cep = edtCep.getText().toString();
    String logradouro = edtLogradouro.getText().toString();
    String bairro = edtBairro.getText().toString();
    String cidade = edtLocalidade.getText().toString();
    String uf = edtUF.getText().toString();

    JSONObject dadosJsonObject = new JSONObject();
    dadosJsonObject.put("nome", edtNome.getText().toString());
    dadosJsonObject.put("sobrenome", edtSobrenome.getText().toString());
    dadosJsonObject.put("celular", edtCelular.getText().toString());
    dadosJsonObject.put("cep", edtCep.getText().toString());
    dadosJsonObject.put("logradouro", edtLogradouro.getText().toString());
    dadosJsonObject.put("bairro", edtBairro.getText().toString());
    dadosJsonObject.put("cidade", edtLocalidade.getText().toString());
    dadosJsonObject.put("uf", edtUF.getText().toString());
    dadosJsonObject.toString();
    //Toast.makeText(getBaseContext(),"Dados retornados: "+dadosJsonObject, Toast.LENGTH_LONG).show();

    //String url =  "http://reservacomdomanda.com/areaAdmin/api/area_admin/usuario.php?dados="+dadosJsonObject;
    URL url = new URL("http://reservacomdomanda.com/areaAdmin/api/area_admin/usuario.php");


}

PHP:

<?php
header("Access-Control-Allow-Origin: *");
ini_set('display_errors', true);
error_reporting(E_ALL);

include_once("con.php");

$pdo = conectar();

$data = file_get_contents("php://input");
$data = json_decode($dadosJsonObject);

print_r($data);
?>
    
asked by anonymous 25.08.2017 / 18:05

1 answer

2

The rough way would be something like

public class MyAsyncTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        URL url = null;
        try {
            String path = (String) params[0];
            String json = (String) params[1];

            url = new URL(path);

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setReadTimeout(30000);
            httpURLConnection.setConnectTimeout(30000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            httpURLConnection.connect();

            OutputStream outputStream = httpURLConnection.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");

            outputStreamWriter.write(json); //dados a seram enviados

            outputStreamWriter.flush();
            outputStreamWriter.close();

            String inputLine = "";

            InputStreamReader streamReader = new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8");

            BufferedReader reader = new BufferedReader(streamReader);

            StringBuilder stringBuilder = new StringBuilder();

            while ((inputLine = reader.readLine()) != null) {
                stringBuilder.append(inputLine);
            }

            reader.close();
            streamReader.close();

            return stringBuilder.toString();

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (ProtocolException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }
}

And call something like:

MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute("http://reservacomdomanda.com/areaAdmin/api/area_admin/usuario.php", dadosJsonObject.toString()); // trocar pelos seus dados

Remembering that I created this dummy AsyncTak that works just for example. I suggest you take a look at AsynkTask to suit your needs.

    
25.08.2017 / 19:21