我进行了验证,以使用改型进行登录,并且我已经将sharedpreferences放在了登录类和第二个类中。可以登录,但是登录时无法获取用户ID。有多种方法,但是没有用。有人知道解决方案吗?
ValidationLogin类
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();
String password = txtpassword.getText().toString();
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<ResponseLogin> call = api.login_member(id, username , password);
call.enqueue(new Callback<ResponseLogin>() {
@Override
public void onResponse(Call<ResponseLogin> call, Response<ResponseLogin> response) {
pDialog.dismiss();
if(response.isSuccessful()){
if(response.body().getResult() != null){
// menyimpan login ke session
sharedpreferences = getSharedPreferences(my_shared_preferences, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(session_status, true);
editor.putString(TAG_ID, id);
editor.putString(TAG_EMAIL, email);
editor.apply();
//login start main activity
Intent intent = new Intent(LoginUser.this, Second.class);
Toast.makeText(LoginUser.this, "Selamat datang "+username, Toast.LENGTH_SHORT).show();
// Toast.makeText(LoginUser.this, "Selamat datang "+id, Toast.LENGTH_SHORT).show();
intent.putExtra("email", email);
intent.putExtra("id", id);
intent.putExtra("username", username);
intent.putExtra(TAG_EMAIL, email);
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<ResponseLogin> call, Throwable t) {
t.printStackTrace();
pDialog.dismiss();
Toast.makeText(LoginUser.this, "Koneksi internet terputus.", Toast.LENGTH_SHORT).show();
}
});
}
onCreate
// Cek session login jika TRUE maka langsung buka MainActivity
sharedpreferences = getSharedPreferences(my_shared_preferences, Context.MODE_PRIVATE);
session = sharedpreferences.getBoolean(session_status, false);
id = sharedpreferences.getString(TAG_ID, "id tidak ditemukan");
email = sharedpreferences.getString(TAG_EMAIL, "email tidak ditemukan");
if (session) {
Intent intent = new Intent(LoginUser.this, MenuUtama.class);
intent.putExtra(TAG_ID, id);
intent.putExtra(TAG_EMAIL, email);
intent.putExtra(TAG_USERNAME, username);
finish();
startActivity(intent);
}
btnLogin = findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = txtusername.getText().toString();
String password = txtpassword.getText().toString();
if (isEmpty(username))
txtusername.setError("Username harap diisi");
else if (isEmpty(password))
txtpassword.setError("Password harap diisi");
else
LoginUser();
//Intent intent = new Intent(LoginUser.this, Second.class);
///startActivity(intent);
}
});
第二堂课
sharedpreferences = getApplication().getSharedPreferences(ValidationLogin.my_shared_preferences, Context.MODE_PRIVATE);
id = sharedpreferences.getString("id", "0");
Toast.makeText(getApplication(), "ini id ke-"+ id, Toast.LENGTH_SHORT).show();
这个响应json,我在努力获取ID
{
"status": 200,
"reason": "OK",
"success": true,
"message": null,
"result": "eyJ0eXAiOiJKV1QiLCJhbGciOiJITUFDLVNIQTI1NiJ9.eyJpZCI6IjQ5OSIsImVtYWlsIjoiYmlzbWlsbGFoYmlzYUBleGFtcGxlLmNvbSIsIm1zaXNkbiI6IjA3OTc5Nzg0NjQ5NCIsInVzZXJuYW1lIjoiYmlzbWlsbGFoYmlzYSIsInZlcmlmaWVkTWVtYmVyIjpudWxsLCJwcm9maWxlIjp7ImlkIjoiMzE2IiwiaWRfZ2VvZGlyZWN0b3J5IjpudWxsLCJmdWxsbmFtZSI6ImJpc21pbGxhaGJpc2EiLCJudW1iZXIiOiIyNzQyNDciLCJpbWFnZSI6Imh0dHBzOlwvXC9kZW1vLmtyZWRpdGltcGlhbi5jb21cL3N0b3JhZ2VcL2ltYWdlc1wvZGVmYXVsdFwvYXZhdGFyLmpwZyIsInJlY29yZCI6eyJzdGF0dXMiOiJQVUJMSVNIIiwiY3JlYXRlIjp7InVzZXIiOm51bGwsInRpbWVzdGFtcCI6eyJkYXRlIjoiMjAxOS0xMi0wMyAxNTowODozMi4wMDAwMDAiLCJ0aW1lem9uZV90eXBlIjozLCJ0aW1lem9uZSI6IkFzaWFcL0pha2FydGEifX0sInVwZGF0ZSI6eyJ1c2VyIjpudWxsLCJ0aW1lc3RhbXAiOm51bGx9fSwibWV0YWRhdGEiOnsidXNlcm5hbWUiOiJiaXNtaWxsYWhiaXNhIiwiZW1haWwiOiJiaXNtaWxsYWhiaXNhQGV4YW1wbGUuY29tIiwicGhvbmUiOiIwNzk3OTc4NDY0OTQifSwic3RhdGlzdGljIjpudWxsfX0.zIEhdU5MyNjReG_9_661FWf0_R5eZuJweyl0JNFd7X0"
}