Handling JSON Response with Retrofit in Android Studio

3

To be very clear I'm developing a simple application test, where my user types the cpf of a registered customer and their data as clients appear on the screen. My server returns me a json with a simple structure like this:

{"id":1,"name":"Lucas","cpf":11111111100,"tel1":33999999999,"empresa":"Empresa 1"}

Using retrofit I make the call normally

@GET("{id}")
Call<Cliente> getCliente(@Path("id")  String  cliente);
public static final  Retrofit retrofit=  new
        Retrofit.Builder().baseUrl("http://meuIP:porta/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

I create a POJO to receive the json data:

public class Cliente{

int id;
public String name;
public String cpf;
public String tel1;
public String empresa;

public String getCpf() {
    return cpf;
}

and in my main class I make the call with a click of a button:

btLogar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // Aqui a função do botão entrar

            RetrofitDados  retrofitDados = RetrofitDados.retrofit.create(RetrofitDados.class);
            final Call<Cliente> call = retrofitDados.getCliente("");


            dialog = new ProgressDialog(MainActivity.this);
            dialog.setMessage("Carregando...");
            dialog.setCancelable(false);
            dialog.show();

            call.enqueue(new Callback<Usuario>() {
                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                @Override
                public void onResponse(Call<Cliente> call, Response<Cliente> response) {

                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                    int code = response.code();
                    //boolean resposta = response.isSuccessful();

                    if (code == 200) {
                        Cliente cliente = response.body();

                        verificarCpf(cliente.cpf);

                    } else {
                        gerarToast("Falha:  " + String.valueOf(code));
                    }
                }

                @Override
                public void onFailure(Call<Cliente> call, Throwable t) {
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
            });
        }
    });

It works perfectly here. When I do the verification of the returned data (in the case of the cpf of the client typed) the program does not check and does not return an error to me.

In my interface there is a TextInputLayout with a TextInputEditText where I type the client's cpf. The program should check the "equality" between what was typed and the response from the server.

private void verificarCpf(final String cpf) {

    String resposta = textCPF.getText().toString();

    if ( resposta == cpf) {
        gerarToast("" + cpf);
    } else {
        Log.e("TAG", resposta);
    }

}

Even my response converted to String

cliente.cpf

and my EditText converted to String

textCPF.getText().toString();

having the same result value. There is no verification.

And what I would like is a hint and a solution, in case I'm forgetting to do something in the code.

    
asked by anonymous 23.11.2018 / 13:39

0 answers