Guest User

Untitled

a guest
Nov 12th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  3. Bundle savedInstanceState) {
  4.  
  5. final View vista = inflater.inflate(R.layout.fragment_sesion, container, false);
  6. txtUser = (EditText) vista.findViewById(R.id.txtuser);
  7. txtPwd = (EditText) vista.findViewById(R.id.txtpwd);
  8. btnSesion = (Button) vista.findViewById(R.id.btnsesion);
  9. saveLoginCheckbox = vista.findViewById(R.id.saveLoginCheckbox);
  10. loginPreferences = getContext().getSharedPreferences("loginPrefs", MODE_PRIVATE);
  11. loginPrefsEditor = loginPreferences.edit();
  12.  
  13. username = txtUser.getText().toString();
  14. password = txtPwd.getText().toString();
  15.  
  16.  
  17. rq = Volley.newRequestQueue(getContext());
  18.  
  19. btnSesion.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View view) {
  22. username = txtUser.getText().toString();
  23. password = txtPwd.getText().toString();
  24.  
  25.  
  26.  
  27. while (username != null || password != null) {
  28.  
  29. btnSesion.setEnabled(true);
  30.  
  31. if (saveLoginCheckbox.isChecked()) {
  32. loginPrefsEditor.putBoolean("saveLogin", true);
  33. loginPrefsEditor.putString("username", username);
  34. loginPrefsEditor.putString("password", password);
  35. loginPrefsEditor.commit();
  36. } else {
  37. loginPrefsEditor.clear();
  38. loginPrefsEditor.commit();
  39. }
  40.  
  41. }
  42.  
  43. btnSesion.setEnabled(false);
  44.  
  45. iniciar_sesion();
  46. }
  47. });
  48.  
  49.  
  50. // Inflate the layout for this fragment
  51. return vista;
  52. }
  53.  
  54.  
  55. @Override
  56. public void onErrorResponse(VolleyError error) {
  57. Toast.makeText(getContext(), "No Se encontró el usuario " + error.toString() + txtUser.getText().toString(), Toast.LENGTH_LONG).show();
  58. }
  59.  
  60. @Override
  61. public void onResponse(JSONObject response) {
  62. Toast.makeText(getContext(), "Se encontró el usuario " + txtUser.getText().toString(), Toast.LENGTH_SHORT).show();
  63.  
  64. User usuario = new User();
  65.  
  66. JSONArray jsonArray = response.optJSONArray("datos");
  67. JSONObject jsonObject = null;
  68.  
  69. try {
  70. saveLogin = loginPreferences.getBoolean("saveLogin", false);
  71. jsonObject = jsonArray.getJSONObject(0);
  72. if (saveLogin == true) {
  73. usuario.setUser(jsonObject.optString("user"));
  74. usuario.setPwd(jsonObject.optString("pwd"));
  75. usuario.setNames(jsonObject.optString("names"));
  76. saveLoginCheckbox.setChecked(true);
  77. }
  78. } catch (JSONException e) {
  79. e.printStackTrace();
  80. }
  81.  
  82.  
  83. Intent intencion = new Intent(getContext(), Main2Activity.class);
  84. intencion.putExtra(Main2Activity.nombres, usuario.getNames());
  85. startActivity(intencion);
  86.  
  87.  
  88. }
  89.  
  90.  
  91. void iniciar_sesion() {
  92. //192.168.1.66(172.29.243.3 https://alonsoh.000webhostapp.com/
  93.  
  94.  
  95. /* String url = "https://alonsoh.000webhostapp.com/sesion.php?user=" + txtUser.getText().toString() +
  96. "&pwd=" + txtPwd.getText().toString();
  97. jrq = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
  98. rq.add(jrq); */
  99.  
  100. JsonObjectRequest jsonObejct = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
  101. @Override
  102. public void onResponse(JSONObject response) {
  103.  
  104. Log.wtf("The Response ", response.toString());
  105. }
  106. }, new Response.ErrorListener() {
  107. @Override
  108. public void onErrorResponse(VolleyError error) {
  109.  
  110.  
  111. }
  112. }) {
  113. @Override
  114. public Map<String, String> getHeaders() throws AuthFailureError{
  115. Map<String, String> params = new HashMap<>();
  116. params.put("Authorization", "MTQxMzE0NEB1dHAuZWR1LnBlOmthaXNlcjEyMw==");
  117. return params;
  118. }
  119.  
  120.  
  121. };
  122. }
Add Comment
Please, Sign In to add comment