Advertisement
Guest User

Is_authAsync

a guest
Oct 17th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. package com.juaracoding.dukcapil_test_api;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.core.app.ActivityCompat;
  5. import androidx.core.content.ContextCompat;
  6.  
  7. import android.Manifest;
  8. import android.app.Activity;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.SharedPreferences;
  12. import android.content.pm.PackageManager;
  13. import android.os.Bundle;
  14. import android.preference.PreferenceManager;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20. import com.juaracoding.dukcapil_test_api.ApiService.ApiClient;
  21. import com.juaracoding.dukcapil_test_api.ApiService.ApiInterfaceREST;
  22. import com.juaracoding.dukcapil_test_api.ApiService.AppUtil;
  23. import com.juaracoding.dukcapil_test_api.Model.Status;
  24.  
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27.  
  28. import java.io.BufferedReader;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.io.InputStreamReader;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34.  
  35. import okhttp3.MediaType;
  36. import okhttp3.RequestBody;
  37. import retrofit2.Call;
  38. import retrofit2.Callback;
  39. import retrofit2.Response;
  40.  
  41. public class Is_AuthAsync extends AppCompatActivity {
  42.  
  43. TextView textview, getToken;
  44. Button loginBtn;
  45.  
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_main);
  50. textview = findViewById(R.id.textView);
  51. getToken = findViewById(R.id.Token);
  52. loginBtn = findViewById(R.id.loginBtn);
  53.  
  54.  
  55. checkAndRequestPermissions(Is_AuthAsync.this);
  56.  
  57. loginBtn.setOnClickListener(new View.OnClickListener() {
  58. @Override
  59. public void onClick(View view) {
  60. getToken();
  61. }
  62. });
  63. }
  64.  
  65.  
  66. public boolean checkAndRequestPermissions(Context context) {
  67.  
  68. List<String> listPermissionsNeeded = new ArrayList();
  69. int writefilePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE);
  70. int cameraPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
  71. if (writefilePermission != PackageManager.PERMISSION_GRANTED) {
  72. listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
  73. }
  74. if (cameraPermission != PackageManager.PERMISSION_GRANTED) {
  75. listPermissionsNeeded.add(Manifest.permission.CAMERA);
  76. }
  77.  
  78. if (!listPermissionsNeeded.isEmpty()) {
  79. ActivityCompat.requestPermissions((Activity) context, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 6969);
  80. return false;
  81. }
  82. return true;
  83. }
  84.  
  85. ApiInterfaceREST apiInterface;
  86. Status status;
  87. private void getToken(){
  88. apiInterface = ApiClient.getClientWithApi().create(ApiInterfaceREST.class);
  89.  
  90. JSONObject rBody = new JSONObject();
  91. try {
  92. rBody.put("default_lang", AppUtil.DEFAULT_LANG);
  93. rBody.put("api_key", AppUtil.API_KEY);
  94. rBody.put("api_secret", AppUtil.API_SECRET);
  95. }catch (JSONException e){
  96.  
  97. }
  98. Call<Status> statusCall = apiInterface.getToken(RequestBody.create(MediaType.parse("application/json"), rBody.toString()));
  99. statusCall.enqueue(new Callback<Status>() {
  100.  
  101. @Override
  102. public void onResponse(Call<Status> call, Response<Status> response) {
  103. status = response.body();
  104. if(status != null){
  105.  
  106. getToken.setText(status.getPayload().getToken());
  107. Toast.makeText(Is_AuthAsync.this, "Token Get!!!", Toast.LENGTH_LONG).show();
  108.  
  109. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Is_AuthAsync.this);
  110. prefs.edit().putString("Token", status.getPayload().getToken()).commit();
  111.  
  112. Intent intent = new Intent(Is_AuthAsync.this, Scan_IDAsync.class);
  113. startActivity(intent);
  114.  
  115. } else {
  116. InputStream i = response.errorBody().byteStream();
  117. BufferedReader r = new BufferedReader(new InputStreamReader(i));
  118. StringBuilder errorResult = new StringBuilder();
  119. String line;
  120. try {
  121. while ((line = r.readLine()) != null) {
  122. errorResult.append(line).append('\n');
  123. }
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. }
  127.  
  128. Toast.makeText(Is_AuthAsync.this, "Login Failed " + errorResult, Toast.LENGTH_LONG).show();
  129. }
  130. }
  131.  
  132. @Override
  133. public void onFailure(Call<Status> call, Throwable t) {
  134. Toast.makeText(getApplicationContext(), "There is connection problem!!!", Toast.LENGTH_LONG).show();
  135. call.cancel();
  136. }
  137. });
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement