Android: Data does not persist in Mysql

Asked

Viewed 33 times

0

Good Afternoon,

With the code below I get a success message when executing the method, but the data is not in the table when validating whether it was persisted in the database (Mysql).

public class Main2Activity extends AppCompatActivity implements 
Response.Listener<JSONObject>, Response.ErrorListener {

EditText edtDocumento, edtNome, edtProfissao;
Button btnCadastrar;
ProgressDialog progressDialog;

RequestQueue request;
JsonObjectRequest jsonObjectRequest;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    btnCadastrar = findViewById(R.id.btnCadastrar);
    edtDocumento = findViewById(R.id.edtDocumento);
    edtNome = findViewById(R.id.edtNome);
    edtProfissao = findViewById(R.id.edtProfissao);

    request  = Volley.newRequestQueue(getApplicationContext());

    btnCadastrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            carregarWebService();


        }
    });

}

private void carregarWebService() {

    String url = "http://192.168.0.1/exemploBDRemota/wsJSONRegistro.php?documento=" + edtDocumento.getText().toString() +
            "&nome=" + edtNome.getText().toString() + "&profissao=" + edtProfissao.getText().toString();

    url = url.replace(" ","%20");


    jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
    request.add(jsonObjectRequest);
}

@Override
public void onResponse(JSONObject response) {
    Toast.makeText(this, "Registrado com Sucesso!", Toast.LENGTH_SHORT).show();
    edtDocumento.setText("");
    edtNome.setText("");
    edtProfissao.setText("");

}

@Override
public void onErrorResponse(VolleyError error) {


    //progressDialog.hide();
    Toast.makeText(this, "Falha ao registrar. " + error.toString(), Toast.LENGTH_SHORT).show();
    Log.i("ERROR",error.toString());
}
}

PHP

$hostname_localhost = "localhost";
$database_localhost = "db_usuario";
$username_localhost = "root";
$senha_localhost = "";

$json = array();

if(isset($_GET["documento"]) && isset($GET["nome"]) && isset($GET["profissao"])){

    $documento = $GET['documento'];
    $nome = $GET['nome'];
    $profissao = $GET['profissao'];

    $conexao = mysqli_connect ($hostname_localhost, $username_localhost, $senha_localhost, $database_localhost);

    $insert = "INSERT INTO usuario(documento, nome, profissao) VALUES ('{$documento}','{$nome}','{$profissao}')";
    $resultado_insert = mysqli_query($conexao, $insert);


    if($resultado_insert){

        $consulta = "SELECT * FROM usuario WHERE documento = '{$documento}'";
        $resultado = mysqli_query($conexao, $consulta);


        if($registro = mysqli_fetch_array($resultado)){
            $json['usuario'][] = $registro;
        }

        mysqli_close($conexao);
        echo json_encode($json);
    }

    else{
        $resulta["documento"] = 0;
        $resulta["nome"] = 'Nao Registrado';
        $resulta["profissao"] = 'Nao Registrada';   
        $json['usuario'][] = $resulta;
        echo json_encode($json);    

    }
}
else{
    $resulta["documento"] = 0;
    $resulta["nome"] = 'WS Nao Retorna';
    $resulta["profissao"] = 'WS Nao Retorna';

    $json['usuario'][] = $resulta;
    echo json_encode($json);    
}
  • onResponse only returns that there was a OK response from the server. mysqli_query($conexao, $insert) or die(mysqli_error($conexao)); and see if there are any errors!

  • Have you considered implementing the webservice with Laravel and i5Repository?

No answers

Browser other questions tagged

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