Exemplo n.º 1
0
  /** Do login */
  private void doLogin() {

    String strPassword = edtPassword.getText().toString();
    String strEmail = edtEmail.getText().toString().toLowerCase();
    CustomPreferences.setPreferences(Constants.PREF_USERNAME, strEmail);
    CustomPreferences.setPreferences(Constants.PREF_PASSWORD, strPassword);
    final LoadingDialog dialog = LoadingDialog.show(this);
    new UserApi()
        .getInterface()
        .doLogin()
        .enqueue(
            new Callback<UserEntity>() {
              @Override
              public void onResponse(Response<UserEntity> response, Retrofit retrofit) {

                finishLogin(response);
                Utilities.dismissDialog(dialog);
              }

              @Override
              public void onFailure(Throwable t) {

                finishLogin(null);
                Utilities.dismissDialog(dialog);
              }
            });
  }
Exemplo n.º 2
0
  public BaseApi() {

    String strUserName = CustomPreferences.getPreferences(Constants.PREF_USERNAME, "");
    String strPassword = CustomPreferences.getPreferences(Constants.PREF_PASSWORD, "");
    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    OkHttpClient httpClient = new OkHttpClient();
    final String credential = Credentials.basic(strUserName, strPassword);
    httpClient
        .interceptors()
        .add(
            new Interceptor() {
              @Override
              public com.squareup.okhttp.Response intercept(Interceptor.Chain chain)
                  throws IOException {

                com.squareup.okhttp.Request original = chain.request();
                com.squareup.okhttp.Request request =
                    original
                        .newBuilder()
                        .header("Authorization", credential)
                        .header("Accept", "application/vnd.textus-v2+json")
                        .method(original.method(), original.body())
                        .build();
                LogUtil.e("Url : ", request.httpUrl().toString());
                com.squareup.okhttp.Response response = chain.proceed(request);
                if (request.body() != null) {

                  Buffer buffer = new Buffer();
                  request.body().writeTo(buffer);
                  String body = buffer.readUtf8();
                  LogUtil.e("body : ", body);
                }

                String strBody = response.body().string();
                LogUtil.e("Response", strBody);
                return response
                    .newBuilder()
                    .body(ResponseBody.create(response.body().contentType(), strBody))
                    .build();
              }
            });

    mRetrofit =
        new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .client(httpClient)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
  }
Exemplo n.º 3
0
  /**
   * Finish login
   *
   * @param response response
   */
  private void finishLogin(Response<UserEntity> response) {

    if (response == null || response.body() == null) {

      CustomPreferences.setPreferences(Constants.PREF_USERNAME, "");
      CustomPreferences.setPreferences(Constants.PREF_PASSWORD, "");
      CustomPreferences.setPreferences(Constants.PREF_USER_ID, 0l);
      CustomPreferences.setPreferences(Constants.PREF_ACC_ID, 0l);
      Utilities.showAlertDialog(
          LoginActivity.this,
          getString(R.string.login_fail),
          getString(R.string.try_again),
          getString(R.string.ok),
          "",
          null,
          null,
          false);
      edtPassword.setText("");
      edtPassword.requestFocus();
      return;
    }

    CustomPreferences.setPreferences(Constants.PREF_USER_ID, response.body().getId());
    CustomPreferences.setPreferences(Constants.PREF_ACC_ID, response.body().getAccountId());
    startActivity(new Intent(this, MainActivity.class));
    finish();
  }