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