Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginFragment extends Fragment implements Response.Listener<JSONObject>, Response.ErrorListener {
- EditText username, password;
- Button btn;
- ProgressDialog progreso;
- RequestQueue request, request2;
- JsonObjectRequest jsonObjectRequest;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View vista = inflater.inflate(R.layout.fragment_login, container, false);
- username = (EditText)vista.findViewById(R.id.input_username);
- password = (EditText)vista.findViewById(R.id.input_password);
- btn = (Button)vista.findViewById(R.id.btn_login);
- request = Volley.newRequestQueue(getContext());
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- cargarWebService();
- }
- });
- return vista;
- }
- private void cargarWebService() {
- progreso = new ProgressDialog(getContext());
- progreso.setMessage("Comprobando datos...");
- progreso.onBackPressed();
- progreso.show();
- String url = "http://192.168.96.1/webservice/login.php?username="+username.getText().toString()+"&password="+password.getText().toString();
- jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, this,this);
- request.add(jsonObjectRequest);
- }
- @Override
- public void onErrorResponse(VolleyError error) {
- progreso.hide();
- Toast.makeText(getContext(), "Datos erroneos. Por favor intente nuevamente "+error.toString(), Toast.LENGTH_SHORT).show();
- Log.i("ERROR", error.toString());
- }
- @Override
- public void onResponse(JSONObject response) {
- progreso.hide();
- Usuario miUsuario = new Usuario();
- problema problema = new problema();
- JSONArray json = response.optJSONArray("usuario");
- JSONArray json2 = response.optJSONArray("problems");
- JSONObject jsonObject = null;
- JSONObject jsonObject1 = null;
- try {
- //Problemas del usuario...
- jsonObject1=json2.getJSONObject(0); //OBTIENE EL INDEX 0. PERO SI SON VARIOS TENDRIA QUE HACER UN FOR. PRIMERO PROBAMOS CON ESTO.
- problema.setSpecialty(jsonObject1.optString("specialty"));
- problema.setDescription(jsonObject1.optString("description"));
- // problema.setFechaIngreso(jsonObject1.optString("created_at"));
- //Datos personales del usuario luego de loguearse. Lo pasamos a la vista userpanel.
- jsonObject=json.getJSONObject(0);
- miUsuario.setId(jsonObject.optString("id"));
- miUsuario.setName(jsonObject.optString("name"));
- miUsuario.setLastName(jsonObject.optString("lastName"));
- miUsuario.setEmail(jsonObject.optString("email"));
- miUsuario.setPhone(jsonObject.optString("phone"));
- miUsuario.setZone(jsonObject.optString("zone"));
- // miUsuario.setDate(jsonObject.optString("date"));
- //Enviando datos por intent hacia la ventanan de userpanel.
- Intent intent = new Intent (getActivity(), userPanel.class);
- intent.putExtra("name", miUsuario.getName());
- intent.putExtra("lastName", miUsuario.getLastName());
- intent.putExtra("email", miUsuario.getEmail());
- intent.putExtra("phone", miUsuario.getPhone());
- intent.putExtra("zone", miUsuario.getZone());
- intent.putExtra("date", miUsuario.getDate());
- intent.putExtra("specialty", problema.getSpecialty());
- intent.putExtra("description", problema.getDescription());
- //intent.putExtra("fecha", problema.getFechaIngreso());
- getActivity().startActivity(intent);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
Add Comment
Please, Sign In to add comment