Guest User

Untitled

a guest
Aug 31st, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. import android.app.ProgressDialog;
  2. import android.content.Intent;
  3. import android.content.SharedPreferences;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. import java.security.cert.CertPathValidatorException;
  13.  
  14. import retrofit2.Call;
  15. import retrofit2.Callback;
  16. import retrofit2.Response;
  17. import retrofit2.Retrofit;
  18. import retrofit2.converter.gson.GsonConverterFactory;
  19.  
  20. public class Login extends AppCompatActivity implements View.OnClickListener {
  21.  
  22. EditText emaillogin,passwordlogin;
  23. Button Loginbtn;
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_login);
  29.  
  30. emaillogin = (EditText) findViewById(R.id.editemail);
  31. passwordlogin = (EditText) findViewById(R.id.passwordlogin);
  32. Loginbtn = (Button) findViewById(R.id.loginbtn);
  33.  
  34. Loginbtn.setOnClickListener(this);
  35. }
  36.  
  37. public void signIn(){
  38. final ProgressDialog progressDialog = new ProgressDialog(this);
  39. progressDialog.setMessage("Sign In...");
  40. progressDialog.show();
  41.  
  42. final String username = emaillogin.getText().toString().trim();
  43. final String password = passwordlogin.getText().toString().trim();
  44.  
  45. Retrofit retrofit = new Retrofit.Builder()
  46. .baseUrl(Api.BASE_URL)
  47. .addConverterFactory(GsonConverterFactory.create())
  48. .build();
  49.  
  50. Api services = retrofit.create(Api.class);
  51.  
  52. Call<Message> call = services.userLogin(username,password);
  53.  
  54. call.enqueue(new Callback<Message>() {
  55.  
  56. progressDialog.dismiss();
  57.  
  58.  
  59. if (!response.body().geterror()){
  60.  
  61. finish();
  62. SharedPrefManager.getmInstance(getApplicationContext()).userLogin(response.body().getHero());
  63.  
  64. Toast.makeText(Login.this, "sucess", Toast.LENGTH_SHORT).show();
  65. //startActivity(new Intent(getApplicationContext(),MainActivity.class));
  66. }
  67. else {
  68. Toast.makeText(Login.this, "Failed", Toast.LENGTH_SHORT).show();
  69. //g.d(password,"password");
  70. }
  71.  
  72. }
  73.  
  74. @Override
  75. public void onFailure(Call<Message> call, Throwable t) {
  76.  
  77. Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
  78.  
  79. }
  80. });
  81. }
  82. public void onClick(View view){
  83.  
  84. if (view == Loginbtn ){
  85.  
  86. signIn();
  87. }
  88. }
  89. }
  90.  
  91. public class SharedPrefManager {
  92.  
  93. private static SharedPrefManager mInstance;
  94. private static Context mCTx;
  95.  
  96. private static final String SHARED_PRE_NAME = "dreamotechretrofit";
  97.  
  98. private static final String KEY_ID = "keyid";
  99. private static final String KEY_USERNAME = "username";
  100. private static final String KEY_PASSWORD = "password";
  101. private static final String UPDATE_AT = "updated_at";
  102. private static final String CREATED_AT = "created_at";
  103.  
  104. private SharedPrefManager(Context context){mCTx = context;}
  105.  
  106. public static synchronized SharedPrefManager getmInstance(Context context){
  107.  
  108. if (mInstance == null){
  109.  
  110. mInstance = new SharedPrefManager(context);
  111. }
  112.  
  113. return mInstance;
  114. }
  115.  
  116. public boolean userLogin(Hero hero){
  117.  
  118. SharedPreferences sharedPrefManager = mCTx.getSharedPreferences(SHARED_PRE_NAME,Context.MODE_PRIVATE);
  119. SharedPreferences.Editor editor = sharedPrefManager.edit();
  120. editor.putString(KEY_USERNAME,hero.getUsername());
  121. editor.putString(KEY_PASSWORD,hero.getPassword());
  122. editor.apply();
  123. return true;
  124.  
  125. }
Add Comment
Please, Sign In to add comment