I'm starting with Android programming and I'm 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'd like to store in the connected global variable. However, the way I did it, below, when leaving the class ConnectaBdTask this [connected] variable always remains false. I have already run in Debug mode and the return from 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;
}
}
}