Advertisement
KhiOs

logactivity

Oct 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. package com.khibar.simaskes;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.app.ProgressDialog;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14.  
  15. import com.khibar.simaskes.apihelper.BaseApiService;
  16. import com.khibar.simaskes.apihelper.UtilsApi;
  17.  
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20.  
  21. import java.io.IOException;
  22.  
  23. import okhttp3.ResponseBody;
  24. import retrofit2.Call;
  25. import retrofit2.Callback;
  26. import retrofit2.Response;
  27.  
  28. public class LoginActivity extends AppCompatActivity {
  29.     EditText etemail, etpassword;
  30.     Button btnlogin, btnregister;
  31.     ProgressDialog loading;
  32.    
  33.     Context mContext;
  34.     BaseApiService mApiService;
  35.  
  36.     @Override
  37.     protected void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.         setContentView(R.layout.activity_login);
  40.        
  41.         mContext = this;
  42.             mApiService = UtilsApi.getApiService(); //meng-init di package apihelper
  43.  
  44.         initComponents();
  45.  
  46.     }
  47.  
  48.     private void initComponents() {
  49.         etemail = findViewById(R.id.et_email);
  50.         etpassword = findViewById(R.id.et_password);
  51.         btnlogin = findViewById(R.id.btn_login);
  52.         btnregister = findViewById(R.id.btn_register);
  53.  
  54.         btnlogin.setOnClickListener(new View.OnClickListener() {
  55.             @Override
  56.             public void onClick(View v) {
  57.                 loading = ProgressDialog.show(mContext, null, "Harap Tunggu ...", true, false);
  58.                 requestLogin();
  59.             }
  60.         });
  61.  
  62.             private void requestLogin() {
  63.                 mApiService.loginRequest(etemail.getText().toString(), etpassword.getText().toString()).enqueue(new Callback<ResponseBody>() {
  64.                     @Override
  65.                     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
  66.                         if (response.isSuccessful()){
  67.                             loading.dismiss();
  68.                             try {
  69.                                 JSONObject jsonRESULTS = new JSONObject(response.body().string());
  70.                                 if (jsonRESULTS.getString("error").equals("false")){
  71.                                     //jika login berhasil nama ada di respone api
  72.                                     Toast.makeText(mContext, "BERHASIL LOGIN", Toast.LENGTH_SHORT).show();
  73.                                     String nama = jsonRESULTS.getJSONObject("user").getString("nama");
  74.                                     Intent intent = new Intent(mContext, MainActivity.class);
  75.                                     intent.putExtra("result_nama", nama);
  76.                                     startActivity(intent);
  77.                                 } else {
  78.                                     //jika gagal login
  79.                                     String error_message = jsonRESULTS.getString("error_msg");
  80.                                     Toast.makeText(mContext, error_message, Toast.LENGTH_SHORT).show();
  81.                                 }
  82.                             } catch (JSONException e) {
  83.                                 e.printStackTrace();
  84.                             } catch (IOException e) {
  85.                                 e.printStackTrace();
  86.                             }
  87.                         } else {
  88.                             loading.dismiss();
  89.                         }
  90.                     }
  91.  
  92.                     @Override
  93.                     public void onFailure(Call<ResponseBody> call, Throwable t) {
  94.                         Log.e("debug", "onFailure: ERROR > " + t.toString());
  95.                         loading.dismiss();
  96.                     }
  97.                 });
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement