Hi, I have a question and they did not know how to help me, I have a webservice that is doing a get that when entered the user name it will allow its access to the application and so will pass the user to the next screen.
This is the code:
public class LoginActivity extends AppCompatActivity {
public static final String PREFS_USER = "Preferencia";
EditText user;
Button salvar;
EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
user = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.senha);
salvar = (Button) findViewById(R.id.salvar);
salvar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences settings = getSharedPreferences(PREFS_USER, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("PrefUser", user.getText().toString());
editor.putString("PrefPass", password.getText().toString());
//Confirma a gravação dos dados
editor.commit();
loadJson(user.getText().toString());
}
});
SharedPreferences settings = getSharedPreferences(PREFS_USER, 0);
user.setText(settings.getString("PrefUser", ""));
password.setText(settings.getString("PrefPass", ""));
}
public void loadJson(String usuario){
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://"+getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString("PrefHost", "") +":8080/FazendaWebservice/webresources/fazenda/")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
AcessoClient client = retrofit.create(AcessoClient.class);
Call<Acesso> call = client.reposForUsuario(usuario);
call.enqueue(new Callback<Acesso>() {
@Override
public void onResponse(Call<Acesso> call, Response<Acesso> response) {
}
@Override
public void onFailure(Call<Acesso> call, Throwable t) {
Toast.makeText(LoginActivity.this, " Erro ao estabelecer conexão"+ "\n"+" Verifique o host inserido"+"\n"+"Por favor tente novamente mais tarde!", Toast.LENGTH_SHORT).show();
}
});
}
Class Acesso
:
public class Acesso {
private String nomeusuario;
private String senhausuario;
private String listaprodutos;
private String vendasonline;
public String getNomeusuario() {
return nomeusuario;
}
public void setNomeusuario(String nomeusuario) {
this.nomeusuario = nomeusuario;
}
public String getSenhausuario() {
return senhausuario;
}
public void setSenhausuario(String senhausuario) {
this.senhausuario = senhausuario;
}
public String getListaprodutos() {
return listaprodutos;
}
public void setListaprodutos(String listaprodutos) {
this.listaprodutos = listaprodutos;
}
public String getVendasonline() {
return vendasonline;
}
public void setVendasonline(String vendasonline) {
this.vendasonline = vendasonline;
}
}
Class AcessoClient
:
public interface AcessoClient {
@GET("Acesso/get/{usuario}")
Call <Acesso> reposForUsuario(
@Path("usuario") String usuario
);
}
JSON returned from WebService
{"nomeusuario":"admin","senhausuario":"yMJsiuiTcpC","listaprodutos":"S","vendasonline":"S"}
I do not know what to put inside the onResponse for it to end up doing the validation, does anyone help me?