好的,我有一个授权应用程序,需要保存客户端ID、客户端密钥、访问令牌和刷新令牌。我尝试保存,但做错了些什么,他只保存所有字段的一个值。请检查代码。
这是一段有点长的代码,sharedPreference 部分在最后。
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();
}}
在最后一个方法中,当我尝试获取数据时,客户端ID会变成
168e3903996c90818ecabaf1c60ff4108a2f6d438e8bcb788a7fe5ef5224d1
,客户端密钥、访问令牌和刷新令牌也会发生这种情况。代码有什么问题?