Passing parameter to another activity with sharedPreference [duplicate]

1

I have a code where I will access a webservice that will be changing the ip weekly, I created a sharedpreference for the user to save the new ip and when that save my activity would pull that ip saved in the shared preference and would end up connecting normally. / p>

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    host = (EditText) findViewById(R.id.hostname);
    button = (Button) findViewById(R.id.enviar);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Main2Activity.this, MainActivity.class);
            startActivity(intent);
        }
    });

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    host.setText(settings.getString("PrefHost", ""));
}
@Override
protected void onStop(){
    super.onStop();

    //Caso o checkbox esteja marcado gravamos o usuário
    CheckBox chkSalvar = (CheckBox)findViewById(R.id.chkSalvar);
    if (chkSalvar.isChecked()){
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("PrefHost", host.getText().toString());

        //Confirma a gravação dos dados
        editor.commit();
    }
}

}

public void loadJson (String fetch) {

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://"+IP SALVO ENTRARÁ AQUI+":8080/FazendaWebservice/webresources/fazenda/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

    GitHubClientBuscar client = retrofit.create(GitHubClientBuscar.class);
    Call<List<GitHubRepoBuscar>> call = client.reposForUser(busca);

    call.enqueue(new Callback<List<GitHubRepoBuscar>>() {
        @Override
        public void onResponse(Call<List<GitHubRepoBuscar>> call, Response<List<GitHubRepoBuscar>> response) {
            pbar.setVisibility(View.GONE);
            List<GitHubRepoBuscar> repos = response.body();
            listView.setAdapter(new GitHubRepoBuscarAdapter(MainActivity.this, repos));
        }

        @Override
        public void onFailure(Call<List<GitHubRepoBuscar>> call, Throwable t) {
            pbar.setVisibility(View.INVISIBLE);
            Toast.makeText(MainActivity.this, "         Erro ao estabelecer conexão" +"\n"+"Por favor tente novamente mais tarde!", Toast.LENGTH_SHORT).show();
        }
    });
}
    
asked by anonymous 29.08.2017 / 20:35

2 answers

2
Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://"+getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString("PrefHost", "")+":8080/FazendaWebservice/webresources/fazenda/")
            .addConverterFactory(GsonConverterFactory.create());
    
29.08.2017 / 20:43
1
   /**
     * Inciamos o SharedPreferences com o mesmo name que salvou
     */
    SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
    /**
    * Vamos pegar o valor da chave. Caso ainda não Tenha salvo o valor, retorna ""
    */
    final String prefHost = settings.getString("PrefHost", "");

    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://"+prefHost+":8080/FazendaWebservice/webresources/fazenda/")
            .addConverterFactory(GsonConverterFactory.create());
    
29.08.2017 / 21:26