Save data from a web service to a global variable on Android

Asked

Viewed 215 times

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.

  • Sounds good, but you can’t store objects

  • 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.

  • That’s right, thanks for the tip Bruno.

2 answers

1

You can create a Singleton class and store the data there, I have a class of this kind here

https://github.com/xanexpt/flickr/blob/master/app/src/main/java/com/badjoras/baamflickr/AppSingleton.java

in that class you create getters and setters for the variables you want to store, then you can access "anywhere" this way

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

EDIT: Don’t do as it says in the previous answer, storing things in the Application class is high hammer...

  • Welcome to the site before you take a look at http://answall.com/tour and improve your question to be better helped

  • I didn’t ask @Marcelodiniz questions, I’m giving answers. The answer that was accepted as correct, is as in the programmers we call hammering code, works but is poorly done

-2


Fala Gabriel,

You must 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;
    }
}

There on your Androidmanifest.xml, you should change the name of the application tag that way:

<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 rescue it anywhere in the app, use:

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

Hugs.

  • There is no way to store this direct response in a global variable?

  • Speak Gabriel, yes, I edited my answer, give a look. Hugs.

  • Thanks Leonardo, thank you very much!! I will test and see if it works. Only but a question, I could save the answer of the webservice directly in that global variable called connected?

  • @Gabrieloliveira, if she was static, yes. Because it belongs to the object and not to the class, when the object dies it dies together (the object in the case is the instance of LoginActivity, that when you turn the mobile aside for example will be replaced by another instance). Ah, and if your application goes down for a while and the operating system decides to kill it to save resources, which is very often, it dies too, whether it’s static or not.

  • Ah got it, Piovezan. Thank you very much personal! I tested what Leonardo Alor and it worked very well, solved my problem!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.