Guest User

Untitled

a guest
Oct 9th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. public class LoginFragment extends Fragment implements Response.Listener<JSONObject>, Response.ErrorListener {
  2.  
  3. EditText username, password;
  4. Button btn;
  5. ProgressDialog progreso;
  6. RequestQueue request, request2;
  7.  
  8. JsonObjectRequest jsonObjectRequest;
  9.  
  10.  
  11. @Override
  12. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  13. Bundle savedInstanceState) {
  14. // Inflate the layout for this fragment
  15.  
  16. View vista = inflater.inflate(R.layout.fragment_login, container, false);
  17. username = (EditText)vista.findViewById(R.id.input_username);
  18. password = (EditText)vista.findViewById(R.id.input_password);
  19. btn = (Button)vista.findViewById(R.id.btn_login);
  20.  
  21. request = Volley.newRequestQueue(getContext());
  22.  
  23. btn.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. cargarWebService();
  27. }
  28. });
  29.  
  30. return vista;
  31. }
  32.  
  33. private void cargarWebService() {
  34. progreso = new ProgressDialog(getContext());
  35. progreso.setMessage("Comprobando datos...");
  36. progreso.onBackPressed();
  37. progreso.show();
  38. String url = "http://192.168.96.1/webservice/login.php?username="+username.getText().toString()+"&password="+password.getText().toString();
  39. jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, this,this);
  40. request.add(jsonObjectRequest);
  41. }
  42.  
  43. @Override
  44. public void onErrorResponse(VolleyError error) {
  45. progreso.hide();
  46. Toast.makeText(getContext(), "Datos erroneos. Por favor intente nuevamente "+error.toString(), Toast.LENGTH_SHORT).show();
  47. Log.i("ERROR", error.toString());
  48. }
  49.  
  50. @Override
  51. public void onResponse(JSONObject response) {
  52. progreso.hide();
  53. Usuario miUsuario = new Usuario();
  54. problema problema = new problema();
  55. JSONArray json = response.optJSONArray("usuario");
  56. JSONArray json2 = response.optJSONArray("problems");
  57. JSONObject jsonObject = null;
  58. JSONObject jsonObject1 = null;
  59. try {
  60.  
  61. //Problemas del usuario...
  62. jsonObject1=json2.getJSONObject(0); //OBTIENE EL INDEX 0. PERO SI SON VARIOS TENDRIA QUE HACER UN FOR. PRIMERO PROBAMOS CON ESTO.
  63. problema.setSpecialty(jsonObject1.optString("specialty"));
  64. problema.setDescription(jsonObject1.optString("description"));
  65. // problema.setFechaIngreso(jsonObject1.optString("created_at"));
  66.  
  67.  
  68.  
  69. //Datos personales del usuario luego de loguearse. Lo pasamos a la vista userpanel.
  70. jsonObject=json.getJSONObject(0);
  71. miUsuario.setId(jsonObject.optString("id"));
  72. miUsuario.setName(jsonObject.optString("name"));
  73. miUsuario.setLastName(jsonObject.optString("lastName"));
  74. miUsuario.setEmail(jsonObject.optString("email"));
  75. miUsuario.setPhone(jsonObject.optString("phone"));
  76. miUsuario.setZone(jsonObject.optString("zone"));
  77. // miUsuario.setDate(jsonObject.optString("date"));
  78.  
  79.  
  80. //Enviando datos por intent hacia la ventanan de userpanel.
  81. Intent intent = new Intent (getActivity(), userPanel.class);
  82. intent.putExtra("name", miUsuario.getName());
  83. intent.putExtra("lastName", miUsuario.getLastName());
  84. intent.putExtra("email", miUsuario.getEmail());
  85. intent.putExtra("phone", miUsuario.getPhone());
  86. intent.putExtra("zone", miUsuario.getZone());
  87. intent.putExtra("date", miUsuario.getDate());
  88. intent.putExtra("specialty", problema.getSpecialty());
  89. intent.putExtra("description", problema.getDescription());
  90. //intent.putExtra("fecha", problema.getFechaIngreso());
  91. getActivity().startActivity(intent);
  92. } catch (JSONException e) {
  93. e.printStackTrace();
  94. }
  95.  
  96. }
Add Comment
Please, Sign In to add comment