Java SharedReferences未保存
好的,我有一个授权应用程序,我需要在其中保存clientID、clientSecret、accessToken和refreshToken。 我试图保存,但我做错了,他只为所有字段保存1个值。请检查代码。 这是一个有点长的代码,SharedReference部分在最后Java SharedReferences未保存,java,android,sharedpreferences,Java,Android,Sharedpreferences,好的,我有一个授权应用程序,我需要在其中保存clientID、clientSecret、accessToken和refreshToken。 我试图保存,但我做错了,他只为所有字段保存1个值。请检查代码。 这是一个有点长的代码,SharedReference部分在最后 public class FragmentRegistration extends Fragment { View mainView; EditText name, username, email, password; Butto
public class FragmentRegistration extends Fragment {
View mainView;
EditText name, username, email, password;
Button button;
ApiClient apiClient = ApiClient.getInstance();
SupportopObj supportopObj = new SupportopObj();
SupportopObjActivate supportopActivate = new SupportopObjActivate();
SupportObjToken supportopToken = new SupportObjToken();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mainView = inflater.inflate
(R.layout.registration, container, false);
username = mainView.findViewById(R.id.username);
email = mainView.findViewById(R.id.email);
password = mainView.findViewById(R.id.password);
name = mainView.findViewById(R.id.name);
button = mainView.findViewById(R.id.register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = name.getText().toString();
String split[] = s.split(" ");
supportopObj.setFirstName(split[0]);
supportopObj.setLastName(split[1]);
supportopObj.setUsername(username.getText().toString());
supportopObj.setEmail(email.getText().toString());
supportopObj.setPassword(password.getText().toString());
supportopActivate.setUsername(supportopObj.getUsername());
supportopActivate.setEmail(supportopObj.getEmail());
supportopActivate.setPassword(supportopObj.getPassword());
supportopActivate.setType("generic");
registerCall();
getSaveData();
}
});
return mainView;
}
public void registerCall() {
Call<ResponseBody> call = apiClient.registration(supportopObj);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
//VersionDataInfo versionDataInfo = response.body();
clientCall();
} else {
Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();
clientCall();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getActivity(), "Error...", Toast.LENGTH_SHORT).show();
}
});
}
public void clientCall() {
Call<ResponseBody> callActive = apiClient.activation(supportopActivate);
callActive.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
String data = response.body().string();
JSONObject obj = new JSONObject(data);
String client_id = obj.getString("client_id");
String client_secret = obj.getString("client_secret");
saveData(client_id);
saveData(client_secret);
tokenCall(client_id, client_secret);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getActivity(), "Error in activation", Toast.LENGTH_SHORT).show();
}
});
}
public void tokenCall(String client_id, final String client_secret) {
Call<ResponseBody> token = apiClient.getToken("password", client_id, client_secret,
supportopActivate.getEmail(), supportopActivate.getPassword());
token.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
String dataAccess = response.body().string();
JSONObject obj = new JSONObject(dataAccess);
String access_token = obj.getString("access_token");
String refresh_token = obj.getString("refresh_token");
String client_id = obj.getString("client_id");
supportopToken.setGrantType("refresh_token");
supportopToken.setClientId(client_id);
supportopToken.setClientSecret(client_secret);
supportopToken.setRefreshToken(refresh_token);
saveData(access_token);
saveData(refresh_token);
newTokenCall();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getActivity(), "Something wrong.....", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getActivity(), "You're on failure", Toast.LENGTH_SHORT).show();
}
});
}
public void newTokenCall() {
Call<ResponseBody> newToken = apiClient.newToken(supportopToken);
newToken.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
Toast.makeText(getActivity(), String.valueOf(response.body()), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getActivity(), "You're on failure getting new Token", Toast.LENGTH_SHORT).show();
}
});
}
public void saveData(String data) {
SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Data", data).apply();
}
public void getSaveData() {
SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data", Context.MODE_PRIVATE);
String client_id = preferences.getString("Data", "client_id");
String client_secret = preferences.getString("Data", "client_secret");
String access_token = preferences.getString("Data", "access_token");
String refresh_token = preferences.getString("Data", "refresh_token");
Toast.makeText(getActivity(), client_id + " " + client_secret
+ " " + access_token + " " + refresh_token,Toast.LENGTH_SHORT).show();
}}
公共类碎片注册扩展了碎片{
主视图;
编辑文本名称、用户名、电子邮件、密码;
按钮;
ApiClient ApiClient=ApiClient.getInstance();
SupportopObj SupportopObj=新的SupportopObj();
SupportopObjActivate supportopActivate=新的SupportopObjActivate();
SupportObjToken supportopToken=新的SupportObjToken();
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
主视图=充气机。充气
(R.布局、注册、容器、虚假);
username=mainView.findviewbyd(R.id.username);
email=mainView.findviewbyd(R.id.email);
password=mainView.findviewbyd(R.id.password);
name=mainView.findviewbyd(R.id.name);
按钮=mainView.findViewById(R.id.register);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串s=name.getText().toString();
字符串拆分[]=s.split(“”);
supportopObj.setFirstName(拆分[0]);
supportopObj.setLastName(拆分[1]);
supportopObj.setUsername(username.getText().toString());
supportopObj.setEmail(email.getText().toString());
supportopObj.setPassword(password.getText().toString());
supportopActivate.setUsername(supportopObj.getUsername());
supportopActivate.setEmail(supportopObj.getEmail());
supportopActivate.setPassword(supportopObj.getPassword());
supportopActivate.setType(“泛型”);
注册表调用();
getSaveData();
}
});
返回主视图;
}
公共无效注册表调用(){
Call Call=apiClient.registration(supportopObj);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
//VersionDataInfo VersionDataInfo=response.body();
clientCall();
}否则{
Toast.makeText(getActivity(),“出错了”,Toast.LENGTH_SHORT.show();
clientCall();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(getActivity(),“Error…”,Toast.LENGTH_SHORT.show();
}
});
}
public void clientCall(){
调用callActive=apiClient.activation(supportopActivate);
callActive.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
试一试{
字符串数据=response.body().String();
JSONObject obj=新的JSONObject(数据);
String client_id=obj.getString(“client_id”);
String client_secret=obj.getString(“client_secret”);
保存数据(客户端id);
保存数据(客户端密码);
tokenCall(客户端id、客户端机密);
}捕获(JSONException | IOException e){
e、 printStackTrace();
}
}否则{
Toast.makeText(getActivity(),“error”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(getActivity(),“激活错误”,Toast.LENGTH_SHORT.show();
}
});
}
公共无效令牌调用(字符串客户端\u id,最终字符串客户端\u机密){
Call token=apiClient.getToken(“密码”、客户机id、客户机机密、,
supportopActivate.getEmail(),supportopActivate.getPassword());
entqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
试一试{
String dataAccess=response.body().String();
JSONObject obj=新的JSONObject(数据访问);
String access_token=obj.getString(“access_token”);
String refresh_token=obj.getString(“refresh_token”);
String client_id=obj.getString(“client_id”);
supportopToken.setGrantType(“刷新令牌”);
supportopToken.setClientId(客户端id);
supportopToken.setClientSecret(客户端密码);
supportopToken.setRefreshToken(刷新令牌);
保存数据(访问令牌);
保存数据(刷新令牌);
newTokenCall();
}捕获(IOException | JSONException e){
e、 printStackTrace();
}
}否则{
Toast.makeText(getActivity(),“有问题…”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(getActivity(),“你失败了”,Toast.LENGTH_SHORT.show();
}
});
}
公共无效newTokenCall(){
调用newToken=apiClient.newToken(supportopToken);
enqueue(newcallback()){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
Toast.makeText(getActivity(),String.valueOf(response.body()),Toast.LENGTH_SHORT.show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(getActivity(),“获取新令牌失败”,Toast.LENGTH_SHORT.show();
}
});
}
公共void saveData(字符串数据){
共享参考优先
public void saveData(String key,String data) {
SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, data).apply();
}
saveData("client_id",client_id);
saveData("client_secret",client_secret);
saveData("access_token",access_token);
saveData("refresh_token",refresh_token);
public void getSaveData() {
SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data", Context.MODE_PRIVATE);
String client_id = preferences.getString("client_id", "client_id");
String client_secret = preferences.getString("client_secret", "client_secret");
String access_token = preferences.getString("access_token", "access_token");
String refresh_token = preferences.getString("refresh_token", "refresh_token");
Toast.makeText(getActivity(), client_id + " " + client_secret
+ " " + access_token + " " + refresh_token,Toast.LENGTH_SHORT).show();
}}
saveData(access_token);
saveData(refresh_token);
saveData(access_token, refresh_token);
public void saveData(String token1, String token2) {
SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Data1", token1).apply();
editor.putString("Data2", token2).apply();
}
public void saveData(String key_name,String data) {
SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key_name, data).apply();
}
saveData("client_id","abcded");
public static final String KEY_ACCESS_TOKEN = "accessToken";
public static final String KEY_REFRESH_TOKEN = "refreshToken";
public static final String KEY_CLIENT_ID = "clientId";
public static final String KEY_CLIENT_SECRET = "clientSecret";
public void saveData(String key,String data) {
SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, data).apply();
}
public void getSaveData() {
SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data", Context.MODE_PRIVATE);
String client_id = preferences.getString(KEY_CLIENT_ID , "client_id");
String client_secret = preferences.getString(KEY_CLIENT_SECRET , "client_secret");
String access_token = preferences.getString(KEY_ACCESS_TOKEN , "access_token");
String refresh_token = preferences.getString(KEY_REFRESH_TOKEN , "refresh_token");
Toast.makeText(getActivity(), client_id + " " + client_secret
+ " " + access_token + " " + refresh_token,Toast.LENGTH_SHORT).show();
}}
saveData(KEY_ACCESS_TOKEN ,access_token);
saveData(KEY_REFRESH_TOKEN ,refresh_token);
saveData(KEY_CLIENT_ID ,client_id);
saveData(KEY_CLIENT_SECRET ,client_secret);
public void saveData(String data) {
SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("client_id",client_id).apply();
editor.putString("client_secret",client_secret).apply();
editor.putString("access_token",access_token).apply();
editor.putString("refresh_token",refresh_token).apply();
}
public void getSaveData() {
SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data", Context.MODE_PRIVATE);
String client_id = preferences.getString("client_id", "client_id");
String client_secret = preferences.getString("client_secret", "client_secret");
String access_token = preferences.getString("access_token", "access_token");
String refresh_token = preferences.getString("refresh_token", "refresh_token");
Toast.makeText(getActivity(), client_id + " " + client_secret
+ " " + access_token + " " + refresh_token,Toast.LENGTH_SHORT).show();
}
editor.putString("Data", data).apply();
public void saveData(String data) {
SharedPreferences preferences =
this.getActivity().getSharedPreferences("Saved_data",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("client_id", data).apply();
}
public void getSaveData() {
SharedPreferences preferences =
this.getActivity().getSharedPreferences("Saved_data",
Context.MODE_PRIVATE);
String client_id = preferences.getString("client_id", null);
Toast.makeText(getActivity(), client_id ,Toast.LENGTH_SHORT).show();
}