Save data from a webservice to a global variable in Android

0

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;

        }

    }
}
    
asked by anonymous 28.06.2016 / 17:54

2 answers

-1

Speak Gabriel,

You should create a class that extends Application , example:

public class MyApplication extends Application {

    private String someVariable;

    public String getSomeVariable() {
        return someVariable;
    }

    public void setSomeVariable(String someVariable) {
        this.someVariable = someVariable;
    }
}

In your AndroidManifest.xml, you should change the name of the application tag, like this:

<application 
  android:name=".MyApplication" 
  android:icon="@drawable/icon" 
  android:label="@string/app_name">

Then, to set a variable, use:

// set
((MyApplication) this.getApplication()).setSomeVariable("foo");

And to redeem it anywhere in the application, use:

// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();

Hugs.

    
28.06.2016 / 18:13
1

You can create a singleton class and save the data there, I have a class of that genre here

p>

In this class you create getters and setters for the variables you want to save, then you can access "on any" side this way

AppSingleton.getInstance().setAdsResponse(response.body());
AppSingleton.getInstance().getAdsById(adId);

EDIT: Do not do as it says in the previous response, saving things in the Application class is high hammer ...

    
01.07.2016 / 14:17