在这种情况下,我想检索id_member,用户名和电子邮件字段。但是存储在SharedPrefManager中的是我尝试在jwt.io中解码的令牌,其中包含id_member,用户名和电子邮件。我需要对结果进行解码并使用它来检索用户和交易明细。 这是我成功登录时的响应。我将其保存在Sharedpreferences中,并尝试检索id_member,但是出现“结果响应”登录名。 https://prnt.sc/qbfcsu
谢谢
Loginuser.java
public void LoginUser() {
//membuat progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
pDialog.setMessage("Tunggu proses login ...");
pDialog.show();
//mengambil data dari edittext
final String username = txtusername.getText().toString().trim();
final String password = txtpassword.getText().toString().trim();
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(50, TimeUnit.SECONDS)
.readTimeout(50, TimeUnit.SECONDS).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL).client(client)
.addConverterFactory(GsonConverterFactory.create(new Gson())).build();
RequestInterface api = retrofit.create(RequestInterface.class);
Call<ResponseLoginSucces> call = api.login_member(username, password);
call.enqueue(new Callback<ResponseLoginSucces>() {
@Override
public void onResponse(Call<ResponseLoginSucces> call, Response<ResponseLoginSucces> response) {
if(response.isSuccessful()){
pDialog.dismiss();
if(response.body().getResult() != null){
//DecodeBase64JWTtoString
///String jsonStr = new String(Base64.getDecoder().decode(response.body().getResult().split("\\.")[1].getBytes()), "UTF-8");
// // Gson
///id = new Gson().fromJson(jsonStr, JsonObject.class).get("id").getAsString();
try {
JWTUtils.decodeJWT(response.body().toString());
}
catch (Exception e)
{
}
// Jika login berhasil
String id = response.body().getResult();
String email = response.body().getResult();
String username = response.body().getResult();
String msisdn = response.body().getResult();
sharedPrefManager.saveSPString(SharedPrefManager.SP_ID, id);
sharedPrefManager.saveSPString(SharedPrefManager.SP_EMAIL, email);
sharedPrefManager.saveSPString(SharedPrefManager.SP_USERNAME, username);
sharedPrefManager.saveSPString(SharedPrefManager.SP_MSISDN, msisdn);
Toast.makeText(getApplicationContext(), "Berhasil Login" +id, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginUser.this, MenuUtama.class);
sharedPrefManager.saveSPBoolean(SharedPrefManager.SP_SUDAH_LOGIN, true);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginUser.this, "The username or password is incorrect", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(LoginUser.this, "Error! Please try again!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseLoginSucces> call, Throwable t) {
t.printStackTrace();
pDialog.dismiss();
Toast.makeText(LoginUser.this, "Koneksi internet terputus.", Toast.LENGTH_SHORT).show();
}
});
}
decodeUtils.java
public class JWTUtils {
public static void decodeJWT(String EncodeString) throws Exception
{
String[] splitstr = EncodeString.split("\\.");
Log.d("","Header "+ getJSon( splitstr[0]));
Log.d("","Payload " +getJSon( splitstr[1]));
}
public static String getJSon(String EncodeString) throws UnsupportedEncodingException
{
byte[] decodebyte = Base64.decode(EncodeString, Base64.URL_SAFE);
return new String(decodebyte, "UTF-8");
}
}
SharedPrefManager.java
public class SharedPrefManager {
public static final String SP_Kreditimpian = "kreditimpian_v2_demo";
public static final String SP_ID = "id";
public static final String SP_EMAIL = "email";
public static final String SP_USERNAME = "username";
public static final String SP_MSISDN ="msisdn";
public static final String SP_SUDAH_LOGIN = "spSudahLogin";
SharedPreferences sp;
SharedPreferences.Editor spEditor;
public SharedPrefManager(Context context){
sp = context.getSharedPreferences(SP_Kreditimpian, Context.MODE_PRIVATE);
spEditor = sp.edit();
}
public void saveSPString(String keySP, String value){
spEditor.putString(keySP, value);
spEditor.commit();
}
public void saveSPInt(String keySP, int value){
spEditor.putInt(keySP, value);
spEditor.commit();
}
public void saveSPBoolean(String keySP, boolean value){
spEditor.putBoolean(keySP, value);
spEditor.commit();
}
public String getSPID(){
return sp.getString(SP_ID, "");
}
public String getSPEmail(){
return sp.getString(SP_EMAIL, "");
}
public String getSpUsername(){
return sp.getString(SP_USERNAME, "");
}
public String getSpMsisdn(){
return sp.getString(SP_MSISDN, "");
}
public Boolean getSPSudahLogin(){
return sp.getBoolean(SP_SUDAH_LOGIN, false);
}
}