Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.khibar.simaskes;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- 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 com.khibar.simaskes.apihelper.BaseApiService;
- import com.khibar.simaskes.apihelper.UtilsApi;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import okhttp3.ResponseBody;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class LoginActivity extends AppCompatActivity {
- EditText etemail, etpassword;
- Button btnlogin, btnregister;
- ProgressDialog loading;
- Context mContext;
- BaseApiService mApiService;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- mContext = this;
- mApiService = UtilsApi.getApiService(); //meng-init di package apihelper
- initComponents();
- }
- private void initComponents() {
- etemail = findViewById(R.id.et_email);
- etpassword = findViewById(R.id.et_password);
- btnlogin = findViewById(R.id.btn_login);
- btnregister = findViewById(R.id.btn_register);
- btnlogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- loading = ProgressDialog.show(mContext, null, "Harap Tunggu ...", true, false);
- requestLogin();
- }
- });
- private void requestLogin() {
- mApiService.loginRequest(etemail.getText().toString(), etpassword.getText().toString()).enqueue(new Callback<ResponseBody>() {
- @Override
- public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
- if (response.isSuccessful()){
- loading.dismiss();
- try {
- JSONObject jsonRESULTS = new JSONObject(response.body().string());
- if (jsonRESULTS.getString("error").equals("false")){
- //jika login berhasil nama ada di respone api
- Toast.makeText(mContext, "BERHASIL LOGIN", Toast.LENGTH_SHORT).show();
- String nama = jsonRESULTS.getJSONObject("user").getString("nama");
- Intent intent = new Intent(mContext, MainActivity.class);
- intent.putExtra("result_nama", nama);
- startActivity(intent);
- } else {
- //jika gagal login
- String error_message = jsonRESULTS.getString("error_msg");
- Toast.makeText(mContext, error_message, Toast.LENGTH_SHORT).show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- loading.dismiss();
- }
- }
- @Override
- public void onFailure(Call<ResponseBody> call, Throwable t) {
- Log.e("debug", "onFailure: ERROR > " + t.toString());
- loading.dismiss();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement