Passing parameter to another Activity with sharedPreference

Asked

Viewed 369 times

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 saving my Activity would pull this IP saved in Shared preference and would end up connecting normally.

@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 search){

    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();
        }
    });
}
  • I don’t understand what you want. xD There’s no question in your question.

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());
  • The first error context.

  • Dear your answer is almost right thanks I’ll edit it, just take the first context.

  • Blz, is it a mistake in the context? I’d have to pass this guy to your class, right?

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());

Browser other questions tagged

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