Ejemplo n.º 1
0
 public void registrarGCM(String gcmToken) {
   String METHOD = "registrarGCM";
   HttpURLConnection urlConnection = null;
   try {
     urlConnection = Connection.getHttpUrlConnection("usuario/registrarGCM");
     urlConnection.setDoOutput(true);
     urlConnection.setRequestMethod("POST");
     urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
     String parametros = "token=" + this.token + "&gcmToken=" + gcmToken;
     OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
     out.write(parametros);
     out.close();
     Log.d(TAG, METHOD + " url= " + parametros);
     int HttpResult = urlConnection.getResponseCode();
     if (HttpResult == HttpURLConnection.HTTP_OK) {
       Log.d(TAG, METHOD + " token GCM enviado al servidor para recibir mensajes ");
     } else {
       Log.w(TAG, METHOD + " respuesta no esperada" + urlConnection.getResponseMessage());
     }
   } catch (IOException e) {
     Log.e(TAG, METHOD + " ERROR ", e);
   } finally {
     if (urlConnection != null) urlConnection.disconnect();
   }
   Log.d(TAG, METHOD + " finalizado.");
 }
Ejemplo n.º 2
0
  public void logout() {
    String METHOD = "logout";

    Log.d(TAG, METHOD + " token " + this.token);

    HttpURLConnection urlConnection = null;
    try {
      urlConnection = Connection.getHttpUrlConnection("usuario/logout");
      urlConnection.setDoOutput(true);
      urlConnection.setRequestMethod("POST");
      urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

      OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
      out.write("token=" + this.token);
      out.close();

      int HttpResult = urlConnection.getResponseCode();
      if (HttpResult == HttpURLConnection.HTTP_OK) {
        this.resetearAtributos();
        Log.d(TAG, METHOD + " usuario deslogueado correctamente.");
      } else {
        Log.w(
            TAG,
            METHOD
                + " respuesta no esperada. Usuario no deslogueado. "
                + urlConnection.getResponseMessage());
      }
    } catch (IOException e) {
      Log.e(TAG, METHOD + " ERROR ", e);
    } finally {
      if (urlConnection != null) urlConnection.disconnect();
    }
  }
Ejemplo n.º 3
0
  public void login() throws WrongPasswordException, UsuarioNoActivoException {
    String METHOD = "login";

    Log.d(TAG, METHOD + " facebookId " + this.facebookId);
    Log.d(TAG, METHOD + " email " + this.email + " password " + this.password);

    HttpURLConnection urlConnection = null;
    try {
      urlConnection = Connection.getHttpUrlConnection("usuario/login");
      urlConnection.setDoOutput(true);
      urlConnection.setRequestMethod("POST");
      urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

      String parametros = "";
      if (this.getFacebookId() != null) parametros += "facebookId=" + this.getFacebookId();
      if (!this.getEmail().isEmpty()) {
        if (this.getFacebookId() != null) parametros += "&email=" + this.getEmail();
        else parametros += "email=" + this.getEmail();
      }
      if (!this.getPassword().isEmpty()) parametros += "&password="******"UTF-8")));
          this.logueado = true;
          Log.d(TAG, METHOD + " usuario logueado correctamente.");
          break;
        case HttpURLConnection.HTTP_BAD_METHOD:
          Log.d(TAG, METHOD + " password incorrecta.");
          throw new WrongPasswordException();
        case HttpURLConnection.HTTP_UNAUTHORIZED:
          Log.d(TAG, METHOD + " password incorrecta.");
          throw new UsuarioNoActivoException();
        default:
          this.logueado = false;
          Log.w(
              TAG,
              METHOD
                  + " respuesta no esperada. Usuario no logueado. "
                  + urlConnection.getResponseMessage());
          break;
      }

    } catch (IOException | JSONException e) {
      Log.e(TAG, METHOD + " ERROR ", e);
    } finally {
      if (urlConnection != null) urlConnection.disconnect();
    }
  }
Ejemplo n.º 4
0
  public void registrarse() {
    String METHOD = "registrarse";

    Log.d(TAG, METHOD + " facebookId " + this.facebookId);

    HttpURLConnection urlConnection = null;
    try {

      urlConnection = Connection.getHttpUrlConnection("usuario");
      urlConnection.setDoOutput(true);
      urlConnection.setRequestMethod("POST");
      urlConnection.setRequestProperty("Content-Type", "application/json");

      JSONObject params = new JSONObject();
      if (this.getFacebookId() != null) params.put("facebookId", this.getFacebookId());
      if (!this.getEmail().isEmpty()) params.put("email", this.getEmail());
      if (!this.getPassword().isEmpty()) params.put("password", this.getPassword());
      if (this.getAutoPublicar() != null) params.put("autoPublicar", this.autopublicar);
      if (this.getOfreceTransito() != null) params.put("ofreceTransito", this.ofreceTransito);
      if (!this.getUsername().isEmpty()) params.put("username", this.getUsername());
      if (this.foto != null) params.put("foto", Imagen.base64DEFAULTFromBytes(this.foto.getImg()));

      params.put("direccion", this.direccion);
      params.put("telefono", this.telefono);
      params.put("latitud", this.latitud);
      params.put("longitud", this.longitud);

      OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
      out.write(params.toString());
      out.close();

      Log.d(TAG, METHOD + " url= " + params.toString());

      int HttpResult = urlConnection.getResponseCode();
      if (HttpResult == HttpURLConnection.HTTP_CREATED) {
        this.jsonToUsuario(
            new JsonReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8")));
        this.logueado = true;
        Log.d(TAG, METHOD + " usuario registrado y logueado correctamente.");
      } else {
        this.logueado = false;
        Log.w(
            TAG,
            METHOD
                + " respuesta no esperada. Usuario no registrado. "
                + urlConnection.getResponseMessage());
      }
    } catch (IOException | JSONException e) {
      Log.e(TAG, METHOD + " ERROR ", e);
    } finally {
      if (urlConnection != null) urlConnection.disconnect();
    }
  }