Advertisement
Guest User

Get_DukcapilInfo_Async

a guest
Oct 22nd, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. package com.juaracoding.dukcapil_test_api;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.recyclerview.widget.LinearLayoutManager;
  5. import androidx.recyclerview.widget.RecyclerView;
  6.  
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.os.Bundle;
  10. import android.preference.PreferenceManager;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import com.juaracoding.dukcapil_test_api.ApiService.ApiClientBearerToken;
  17. import com.juaracoding.dukcapil_test_api.ApiService.ApiInterfaceREST;
  18. import com.juaracoding.dukcapil_test_api.Model.Datum;
  19. import com.juaracoding.dukcapil_test_api.Model.Status;
  20.  
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23.  
  24. import java.util.List;
  25.  
  26. import okhttp3.MediaType;
  27. import okhttp3.RequestBody;
  28. import retrofit2.Call;
  29. import retrofit2.Callback;
  30. import retrofit2.Response;
  31.  
  32. public class Get_Dukcapil_InfoAsync extends AppCompatActivity {
  33.  
  34. RecyclerView rv;
  35. TextView textview, nikValid;
  36. Button validBtn, vidPage;
  37. List<Datum> Datum;
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_get__dukcapil__info_async);
  42. rv = findViewById(R.id.rv);
  43. textview = findViewById(R.id.textview);
  44. nikValid = findViewById(R.id.nikValid);
  45. validBtn = findViewById(R.id.vlidBtn);
  46. vidPage = findViewById(R.id.vidPage);
  47.  
  48. final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  49. String nik = prefs.getString("nik", "nik");
  50. nikValid.setText(nik);
  51.  
  52. validBtn.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View view) {
  55. getAllInfo();
  56.  
  57. }
  58. });
  59.  
  60. vidPage.setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View view) {
  63. if(status != null) {
  64. Intent intent = new Intent(Get_Dukcapil_InfoAsync.this, Compare_Faces.class);
  65. startActivity(intent);
  66. }else{
  67. Toast.makeText(getApplicationContext(), "Your input data is invalid", Toast.LENGTH_LONG);
  68. }
  69. }
  70. });
  71.  
  72.  
  73. }
  74.  
  75. ApiInterfaceREST apiInterface;
  76. Status status;
  77. public void getAllInfo(){
  78. apiInterface = ApiClientBearerToken.getClientWithApi().create(ApiInterfaceREST.class);
  79. final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  80. String token = prefs.getString("Token", "Token");
  81. String nik = prefs.getString("nik", "nik");
  82. JSONObject rBody = new JSONObject();
  83. try {
  84. rBody.put("nik", nik);
  85. }catch (JSONException e){
  86.  
  87. }
  88. Call<Status> statusCall = apiInterface.getInfo("Bearer " + token, RequestBody.create(MediaType.parse("application/json"), rBody.toString()));
  89. statusCall.enqueue(new Callback<Status>() {
  90. @Override
  91. public void onResponse(Call<Status> call, Response<Status> response) {
  92. status = response.body();
  93. if(status != null){
  94. Datum = status.getPayload().getData().getData();
  95. setupAdapterList(Datum);
  96. } else{
  97.  
  98. try {
  99. JSONObject jObjError = new JSONObject(response.errorBody().string());
  100. Toast.makeText(Get_Dukcapil_InfoAsync.this, jObjError.getString("message"), Toast.LENGTH_LONG).show();
  101. } catch (Exception e) {
  102. Toast.makeText(Get_Dukcapil_InfoAsync.this, e.getMessage(), Toast.LENGTH_LONG).show();
  103. }
  104. }
  105. }
  106.  
  107. @Override
  108. public void onFailure(Call<Status> call, Throwable t) {
  109. Toast.makeText(getApplicationContext(),"There is problem with your connection!!!",Toast.LENGTH_LONG).show();
  110. call.cancel();
  111. }
  112. });
  113. }
  114.  
  115.  
  116. public void setupAdapterList(List<Datum> model){
  117.  
  118. Get_Dukcpil_InfoAsync_Adapter toadapter = new Get_Dukcpil_InfoAsync_Adapter (Get_Dukcapil_InfoAsync.this, model);
  119. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(Get_Dukcapil_InfoAsync.this, LinearLayoutManager.VERTICAL, false);
  120. rv.setLayoutManager(linearLayoutManager);
  121.  
  122. rv.setAdapter(toadapter);
  123.  
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement