4
To be clear, I am developing a simple application test, where my user type the Cpf of a registered customer and their data as customers 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 data from json:
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 by clicking 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 up to there. When I check the returned data (in case the client Cpf entered) the program does not check and does not resume an error.
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 server response.
private void verificarCpf(final String cpf) {
String resposta = textCPF.getText().toString();
if ( resposta == cpf) {
gerarToast("" + cpf);
} else {
Log.e("TAG", resposta);
}
}
even my answer converted to String
cliente.cpf
and my Edittext converted to String
textCPF.getText().toString();
having the same result value. No verification.
And what I’d like is a hint and a solution, in case I’m forgetting to do something in the code.
This if comparison ( answer == Cpf) is wrong. So you are comparing the response object with the Cpf object and they are not equal. You should compare this: if ( reply.equals(Cpf))
– Reginaldo Rigo
I knew I was forgetting something... THANK YOU
– Lucas Afonso Bastos