Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.security.cert.CertPathValidatorException;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class Login extends AppCompatActivity implements View.OnClickListener {
- EditText emaillogin,passwordlogin;
- Button Loginbtn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- emaillogin = (EditText) findViewById(R.id.editemail);
- passwordlogin = (EditText) findViewById(R.id.passwordlogin);
- Loginbtn = (Button) findViewById(R.id.loginbtn);
- Loginbtn.setOnClickListener(this);
- }
- public void signIn(){
- final ProgressDialog progressDialog = new ProgressDialog(this);
- progressDialog.setMessage("Sign In...");
- progressDialog.show();
- final String username = emaillogin.getText().toString().trim();
- final String password = passwordlogin.getText().toString().trim();
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(Api.BASE_URL)
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- Api services = retrofit.create(Api.class);
- Call<Message> call = services.userLogin(username,password);
- call.enqueue(new Callback<Message>() {
- progressDialog.dismiss();
- if (!response.body().geterror()){
- finish();
- SharedPrefManager.getmInstance(getApplicationContext()).userLogin(response.body().getHero());
- Toast.makeText(Login.this, "sucess", Toast.LENGTH_SHORT).show();
- //startActivity(new Intent(getApplicationContext(),MainActivity.class));
- }
- else {
- Toast.makeText(Login.this, "Failed", Toast.LENGTH_SHORT).show();
- //g.d(password,"password");
- }
- }
- @Override
- public void onFailure(Call<Message> call, Throwable t) {
- Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
- }
- });
- }
- public void onClick(View view){
- if (view == Loginbtn ){
- signIn();
- }
- }
- }
- public class SharedPrefManager {
- private static SharedPrefManager mInstance;
- private static Context mCTx;
- private static final String SHARED_PRE_NAME = "dreamotechretrofit";
- private static final String KEY_ID = "keyid";
- private static final String KEY_USERNAME = "username";
- private static final String KEY_PASSWORD = "password";
- private static final String UPDATE_AT = "updated_at";
- private static final String CREATED_AT = "created_at";
- private SharedPrefManager(Context context){mCTx = context;}
- public static synchronized SharedPrefManager getmInstance(Context context){
- if (mInstance == null){
- mInstance = new SharedPrefManager(context);
- }
- return mInstance;
- }
- public boolean userLogin(Hero hero){
- SharedPreferences sharedPrefManager = mCTx.getSharedPreferences(SHARED_PRE_NAME,Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPrefManager.edit();
- editor.putString(KEY_USERNAME,hero.getUsername());
- editor.putString(KEY_PASSWORD,hero.getPassword());
- editor.apply();
- return true;
- }
Add Comment
Please, Sign In to add comment