0
i am starting with Android programming and am having a problem. I would like to store the response of my webservice in a global variable. This webservice returns a Boolean value, this value I would like to store in the connected global variable. But the way I did, below, when leaving the class Conectabdtask this variable [connected] is always false. I have already run in Debug mode and the return of the webservice is true. Could you help me solve this problem? Thanks!
public class LoginActivity extends Activity {
private boolean conectado;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
conectado = false;
new ConectaBdTask().execute();
}
private class ConectaBdTask extends AsyncTask<Void, Void, Boolean> {
//quando doInBackground termina, é chamado o onPostExecute com o retorno do doInBackground
@Override
protected Boolean doInBackground(Void... params) {
try {
final String url = "localhost/conectarBd";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
//faz a requisição ao Web Service
Boolean conectado = restTemplate.getForObject(url, Boolean.class);
return conectado;
} catch (Exception e) {
Log.e("MainActivity", e.getMessage(), e);
}
return Boolean.FALSE;
}
protected void onPostExecute(Boolean conectado) {
LoginActivity.this.conectado = conectado;
}
}
}
Search Sharedpreferences android on google, is one of the easiest ways to do this.
– Bruno Romualdo
Sounds good, but you can’t store objects
– Gabriel Oliveira
But it’s just Voce keeps the Boolean value and checks if it’s real, check it or leave it unchecked. I use this example to see if the person left a checkbox that saves the password on my login screen.
– Bruno Romualdo
That’s right, thanks for the tip Bruno.
– Gabriel Oliveira