Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.juaracoding.dukcapil_test_api;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.juaracoding.dukcapil_test_api.ApiService.ApiClientBearerToken;
- import com.juaracoding.dukcapil_test_api.ApiService.ApiInterfaceREST;
- import com.juaracoding.dukcapil_test_api.Model.Datum;
- import com.juaracoding.dukcapil_test_api.Model.Status;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.List;
- import okhttp3.MediaType;
- import okhttp3.RequestBody;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class Get_Dukcapil_InfoAsync extends AppCompatActivity {
- RecyclerView rv;
- TextView textview, nikValid;
- Button validBtn, vidPage;
- List<Datum> Datum;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_get__dukcapil__info_async);
- rv = findViewById(R.id.rv);
- textview = findViewById(R.id.textview);
- nikValid = findViewById(R.id.nikValid);
- validBtn = findViewById(R.id.vlidBtn);
- vidPage = findViewById(R.id.vidPage);
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- String nik = prefs.getString("nik", "nik");
- nikValid.setText(nik);
- validBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- getAllInfo();
- }
- });
- vidPage.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if(status != null) {
- Intent intent = new Intent(Get_Dukcapil_InfoAsync.this, Compare_Faces.class);
- startActivity(intent);
- }else{
- Toast.makeText(getApplicationContext(), "Your input data is invalid", Toast.LENGTH_LONG);
- }
- }
- });
- }
- ApiInterfaceREST apiInterface;
- Status status;
- public void getAllInfo(){
- apiInterface = ApiClientBearerToken.getClientWithApi().create(ApiInterfaceREST.class);
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- String token = prefs.getString("Token", "Token");
- String nik = prefs.getString("nik", "nik");
- JSONObject rBody = new JSONObject();
- try {
- rBody.put("nik", nik);
- }catch (JSONException e){
- }
- Call<Status> statusCall = apiInterface.getInfo("Bearer " + token, RequestBody.create(MediaType.parse("application/json"), rBody.toString()));
- statusCall.enqueue(new Callback<Status>() {
- @Override
- public void onResponse(Call<Status> call, Response<Status> response) {
- status = response.body();
- if(status != null){
- Datum = status.getPayload().getData().getData();
- setupAdapterList(Datum);
- } else{
- try {
- JSONObject jObjError = new JSONObject(response.errorBody().string());
- Toast.makeText(Get_Dukcapil_InfoAsync.this, jObjError.getString("message"), Toast.LENGTH_LONG).show();
- } catch (Exception e) {
- Toast.makeText(Get_Dukcapil_InfoAsync.this, e.getMessage(), Toast.LENGTH_LONG).show();
- }
- }
- }
- @Override
- public void onFailure(Call<Status> call, Throwable t) {
- Toast.makeText(getApplicationContext(),"There is problem with your connection!!!",Toast.LENGTH_LONG).show();
- call.cancel();
- }
- });
- }
- public void setupAdapterList(List<Datum> model){
- Get_Dukcpil_InfoAsync_Adapter toadapter = new Get_Dukcpil_InfoAsync_Adapter (Get_Dukcapil_InfoAsync.this, model);
- LinearLayoutManager linearLayoutManager = new LinearLayoutManager(Get_Dukcapil_InfoAsync.this, LinearLayoutManager.VERTICAL, false);
- rv.setLayoutManager(linearLayoutManager);
- rv.setAdapter(toadapter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement