Для сохранения идентификатора сеанса на локальном устройстве используйте Shared Preferences. Здесь используется общий префикс, например, для сохранения и получения идентификатора сеанса.
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPrefManager {
private static final String PREF_NAME = "AndroidHivePref";
SharedPreferences pref;
// Editor for Shared preferences
SharedPreferences.Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
public SharedPrefManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void savesession(String sessionid){
editor.putString("SESSION_ID",session);
editor.commit();
}
public String getsessionid(){
String sessionid = pref.getString("SESSION_ID", null);
return sessionid;
}
}
Использование
//To save Sessionid from onResponse method
...
@Override
public void onResponse(Call<RegResponse> call, Response<RegResponse>response) {
// Get sessionid from response object.
...
SharedPrefManager manager = new SharedPrefManager(getApplicationContext());
//Context object is require to create its object.
...
manager.savesessionid(sessionid);//sessionid is string object got from response above
}
....
// When you want to send sessionid to server either in request data or in request header, get sessionid string object from SharedPrefManager and append it to request object.
SharedPrefManager manager = new SharedPrefManager(getApplicationContext());
String sessionid = manager.getsessionid();