I'm using Robospice to manage my API and I would like to implement a feature on my login screen which allows to change which server URL will be used in my app.
On this login screen activity I have different editText field: username, login and url (server) and a login button.
I've implemented m Robospice service:
public class MyRetrofitService extends RetrofitGsonSpiceService {
public static final String BASE_URL = "https://myUrl.com";
public static final String PREF_FILE_NAME = "PrefFile";
public static final String PREF_USERNAME = "username";
public static final String PREF_PASSWORD = "password";
public static final String PREF_URL = "url";
@Override
public void onCreate() {
super.onCreate();
addRetrofitInterface(MyInterface.class);
}
@Override
protected String getServerUrl() {
// SharedPreferences return my current url saved by the user
String serverUrl = getApplicationContext().getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE).getString("url",BASE_URL);
// I want to change this value
return serverUrl;
}
}
Here my login Activity:
public class WelcomeActivity extends Activity {
public SpiceManager spiceManager = new SpiceManager(MyRetrofitService.class);
@Override
protected void onStart() {
super.onStart();
spiceManager.start(this);
}
@Override
protected void onStop() {
super.onStop();
spiceManager.shouldStop();
}
@Override
protected void onCreate(Bundle savedInstanceState){
// Initialise UI + sharedPreference
....
}
View.OnClickListener loginButtonListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// get data from UI EditText
setUrl(urlAddress.getText().toString())
login(usersname.getText().toString(), password.getText().toString());
}
};
public void setURL(String url) {
// Save login details in sharedPreferences
Auxiliar.editor.putString(MyRetrofitService.PREF_URL, url);
Auxiliar.editor.putString(MyRetrofitService.PREF_USERNAME, usersname.getText().toString());
Auxiliar.editor.putString(MyRetrofitService.PREF_PASSWORD, password.getText().toString());
Auxiliar.editor.commit();
Intent myservice = new Intent(this, MyRetrofitService.class);
stopService(myservice);
spiceManager.shouldStop();
spiceManager = new SpiceManager(MyRetrofitService.class);
spiceManager.start(getBaseContext()); // at this point should trigger getServerURl() in my RobospiceService but it doesn't
}
private void login() {
LoginService loginRequest = new LoginService(username, password);
spiceManager.execute(loginRequest, new HTTPRequestListener());
}
}
So what I'm doing is to stop my SpiceManager service and restart this service to force to trigger this getServerURL method in MyRetrofitService class and change the url.
I checked and this service is really stopped and than restarted but it didn't trigger getServerURl() to change this url.
Am I doing something wrong ? Do you have an idea to change this URL and save the new one ?
The RetrofitSpiceService
is not designed for a requirement like that (see code for details).
I would suggest either overriding the getRetrofitService()
method and provide an implementation which is going to build a separate RestAdapter
for each different server URL or implementing your own RetrofitSpiceService
similar to the existing implementation but addressing your needs.