Advertisement
Raditya48

login.activity

Dec 28th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. package com.qldejozz;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.SimpleAdapter;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import com.android.volley.DefaultRetryPolicy;
  15. import com.android.volley.Response;
  16. import com.android.volley.VolleyError;
  17. import com.android.volley.VolleyLog;
  18. import com.android.volley.toolbox.JsonArrayRequest;
  19. import com.android.volley.toolbox.JsonObjectRequest;
  20. import com.qldejozz.Helper.Http;
  21. import com.qldejozz.Helper.SessionManager;
  22. import com.qldejozz.app.AppController;
  23.  
  24. import org.json.JSONArray;
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27.  
  28. import java.util.HashMap;
  29.  
  30. import cn.pedant.SweetAlert.SweetAlertDialog;
  31.  
  32. import static com.qldejozz.RegisterActivity.url;
  33.  
  34. public class LoginActivity extends AppCompatActivity {
  35.  
  36. EditText username,password;
  37. Button btn_login,btn_toRegister;
  38. TextView lupa_password;
  39. private SessionManager sessionManager;
  40. String load_type;
  41.  
  42. private void findID() {
  43. username = findViewById(R.id.username);
  44. password = findViewById(R.id.password);
  45. btn_login = findViewById(R.id.btn_login);
  46. btn_toRegister = findViewById(R.id.btn_toRegister);
  47. lupa_password = findViewById(R.id.lupa_password);
  48. }
  49.  
  50. @Override
  51. protected void onCreate(Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. setContentView(R.layout.activity_login);
  54. findID();
  55.  
  56. sessionManager = new SessionManager(this);
  57.  
  58. if (sessionManager.isLoggedIn()) {
  59. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  60. startActivity(intent);
  61. finish();
  62. }
  63.  
  64. btn_toRegister.setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View view) {
  67. Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
  68. startActivity(intent);
  69. finish();
  70. }
  71. });
  72.  
  73. btn_login.setOnClickListener(new View.OnClickListener() {
  74. @Override
  75. public void onClick(View view) {
  76. if (username.getText().toString().isEmpty()){
  77. Toast.makeText(LoginActivity.this, "Username kosong...!", Toast.LENGTH_LONG).show();
  78. }else if (password.getText().toString().isEmpty()){
  79. Toast.makeText(LoginActivity.this, "Password kosong...!", Toast.LENGTH_LONG).show();
  80. }else {
  81. checkLogin();
  82. }
  83. // load_type = "login";
  84. // url = Http.getUrl();
  85. // url = url+"GetData?TableName=LOGIN&sOid="+username.getText().toString()+";"+password.getText().toString();
  86. // Log.e("loadProvinsi: ", url);
  87. // accessWebService();
  88. }
  89. });
  90. }
  91.  
  92. private void checkLogin() {
  93. // Toast.makeText(LoginActivity.this, url + username + "/" + password, Toast.LENGTH_LONG).show();
  94. // loadingDialog.hide();
  95. // SweetAlertDialog alert = new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE);
  96. // alert.setTitleText("Oops...!");
  97. // alert.setContentText("username dan password salah!!");
  98. // alert.show();
  99.  
  100. JsonArrayRequest req = new JsonArrayRequest(url+"GetData?TableName=LOGIN&sOid="+username.getText().toString()+";"+password.getText().toString(),
  101. new Response.Listener<JSONArray>(){
  102. @Override
  103. public void onResponse(JSONArray jsonArray) {
  104. Log.e("Coba hasil", "TestHasil" + String.valueOf(jsonArray));
  105. try {
  106. for (int i = 0; i < jsonArray.length(); i++) {
  107. JSONObject jsonObject = jsonArray.getJSONObject(i);
  108. String status = jsonObject.getString("status");
  109. String reason = jsonObject.getString("reason");
  110. if (status.equals("OK")){
  111. sessionManager.setLogin(true,status,username.getText().toString());
  112. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  113. startActivity(intent);
  114. finish();
  115. }else if(status.equals("FAIL")){
  116. Toast.makeText(LoginActivity.this, reason, Toast.LENGTH_LONG).show();
  117. }
  118. }
  119.  
  120. } catch (JSONException e) {
  121. e.printStackTrace();
  122. Toast.makeText(getApplicationContext(),
  123. "Error: " + e.getMessage(),
  124. Toast.LENGTH_LONG).show();
  125. }
  126. }
  127. }, new Response.ErrorListener(){
  128. @Override
  129. public void onErrorResponse(VolleyError error) {
  130. VolleyLog.d("Error: " + error.getMessage());
  131. Toast.makeText(getApplicationContext(),
  132. "Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
  133. }
  134. });
  135.  
  136. req.setRetryPolicy(new DefaultRetryPolicy(30 * 1000, 1, 1.0f));
  137. AppController.getInstance().addToRequestQueue(req);
  138. }
  139.  
  140. private void accessWebService() {
  141. if(load_type.equals("login")){
  142. JsonArrayRequest arrReq = new JsonArrayRequest(url,
  143. new Response.Listener<JSONArray>() {
  144. @Override
  145. public void onResponse(JSONArray response) {
  146. try {
  147. for (int i = 0; i < response.length(); i++) {
  148. JSONObject jsonObject = response.getJSONObject(i);
  149. String status = jsonObject.getString("status");
  150. String reason = jsonObject.getString("reason");
  151. if (status.equals("OK")){
  152. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  153. intent.putExtra("user", username.getText().toString());
  154. startActivity(intent);
  155. }else if(status.equals("FAIL")){
  156. Toast.makeText(LoginActivity.this, reason, Toast.LENGTH_LONG).show();
  157. }
  158. }
  159. }catch (JSONException e) {
  160. e.printStackTrace();
  161. Toast.makeText(getApplicationContext(),
  162. "Error: " + e.getMessage(),
  163. Toast.LENGTH_LONG).show();
  164. }
  165.  
  166. }
  167. }, new Response.ErrorListener() {
  168.  
  169. @Override
  170. public void onErrorResponse(VolleyError error) {
  171. VolleyLog.d("Error: " + error.getMessage());
  172. Toast.makeText(LoginActivity.this,"error : lod"+error.getMessage(),Toast.LENGTH_LONG).show();
  173. //mSwipeRefreshLayout.setRefreshing(false);
  174. }
  175. });
  176.  
  177. // Adding request to request queue
  178. AppController.getInstance().addToRequestQueue(arrReq);
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement