Search in sources :

Example 1 with HttpStatusException

use of org.edx.mobile.http.HttpStatusException in project edx-app-android by edx.

the class LoginActivity method callServerForLogin.

public void callServerForLogin() {
    if (!NetworkUtil.isConnected(this)) {
        showAlertDialog(getString(R.string.no_connectivity), getString(R.string.network_not_connected));
        return;
    }
    final String emailStr = activityLoginBinding.emailEt.getText().toString().trim();
    final String passwordStr = activityLoginBinding.passwordEt.getText().toString().trim();
    if (activityLoginBinding.emailEt != null && emailStr.length() == 0) {
        showAlertDialog(getString(R.string.login_error), getString(R.string.error_enter_email));
        activityLoginBinding.emailEt.requestFocus();
    } else if (activityLoginBinding.passwordEt != null && passwordStr.length() == 0) {
        showAlertDialog(getString(R.string.login_error), getString(R.string.error_enter_password));
        activityLoginBinding.passwordEt.requestFocus();
    } else {
        activityLoginBinding.emailEt.setEnabled(false);
        activityLoginBinding.passwordEt.setEnabled(false);
        activityLoginBinding.forgotPasswordTv.setEnabled(false);
        activityLoginBinding.endUserAgreementTv.setEnabled(false);
        LoginTask logintask = new LoginTask(this, activityLoginBinding.emailEt.getText().toString().trim(), activityLoginBinding.passwordEt.getText().toString()) {

            @Override
            public void onSuccess(@NonNull AuthResponse result) {
                onUserLoginSuccess(result.profile);
            }

            @Override
            public void onException(Exception ex) {
                if (ex instanceof HttpStatusException && ((HttpStatusException) ex).getStatusCode() == HttpStatus.UNAUTHORIZED) {
                    onUserLoginFailure(new LoginException(new LoginErrorMessage(getString(R.string.login_error), getString(R.string.login_failed))), null, null);
                } else {
                    onUserLoginFailure(ex, null, null);
                }
            }
        };
        tryToSetUIInteraction(false);
        logintask.setProgressDialog(activityLoginBinding.progress.progressIndicator);
        logintask.execute();
    }
}
Also used : LoginErrorMessage(org.edx.mobile.exception.LoginErrorMessage) LoginTask(org.edx.mobile.authentication.LoginTask) HttpStatusException(org.edx.mobile.http.HttpStatusException) LoginException(org.edx.mobile.exception.LoginException) LoginException(org.edx.mobile.exception.LoginException) HttpStatusException(org.edx.mobile.http.HttpStatusException) AuthResponse(org.edx.mobile.authentication.AuthResponse)

Example 2 with HttpStatusException

use of org.edx.mobile.http.HttpStatusException in project edx-app-android by edx.

the class LoginActivity method onUserLoginFailure.

public void onUserLoginFailure(Exception ex, String accessToken, String backend) {
    tryToSetUIInteraction(true);
    if (ex != null && ex instanceof LoginException) {
        LoginErrorMessage errorMessage = (((LoginException) ex).getLoginErrorMessage());
        showAlertDialog(errorMessage.getMessageLine1(), (errorMessage.getMessageLine2() != null) ? errorMessage.getMessageLine2() : getString(R.string.login_failed));
    } else if (ex != null && ex instanceof HttpStatusException && ((HttpStatusException) ex).getStatusCode() == HttpStatus.UPGRADE_REQUIRED) {
        LoginActivity.this.showAlertDialog(null, getString(R.string.app_version_unsupported_login_msg), getString(R.string.label_update), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                AppStoreUtils.openAppInAppStore(LoginActivity.this);
            }
        }, getString(android.R.string.cancel), null);
    } else {
        showAlertDialog(getString(R.string.login_error), ErrorUtils.getErrorMessage(ex, LoginActivity.this));
        logger.error(ex);
    }
}
Also used : LoginErrorMessage(org.edx.mobile.exception.LoginErrorMessage) DialogInterface(android.content.DialogInterface) LoginException(org.edx.mobile.exception.LoginException) HttpStatusException(org.edx.mobile.http.HttpStatusException) OnClickListener(android.view.View.OnClickListener)

Example 3 with HttpStatusException

use of org.edx.mobile.http.HttpStatusException in project edx-app-android by edx.

the class MyCoursesListFragment method onLoadFinished.

@Override
public void onLoadFinished(Loader<AsyncTaskResult<List<EnrolledCoursesResponse>>> asyncTaskResultLoader, AsyncTaskResult<List<EnrolledCoursesResponse>> result) {
    adapter.clear();
    final Exception exception = result.getEx();
    if (exception != null) {
        if (exception instanceof AuthException) {
            loginPrefs.clear();
            getActivity().finish();
        } else if (exception instanceof HttpStatusException) {
            final HttpStatusException httpStatusException = (HttpStatusException) exception;
            switch(httpStatusException.getStatusCode()) {
                case HttpStatus.UNAUTHORIZED:
                    {
                        environment.getRouter().forceLogout(getContext(), environment.getAnalyticsRegistry(), environment.getNotificationDelegate());
                        break;
                    }
            }
        } else {
            logger.error(exception);
        }
        errorNotification.showError(getActivity(), exception, R.string.lbl_reload, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (NetworkUtil.isConnected(getContext())) {
                    onRefresh();
                }
            }
        });
    } else if (result.getResult() != null) {
        ArrayList<EnrolledCoursesResponse> newItems = new ArrayList<EnrolledCoursesResponse>(result.getResult());
        updateDatabaseAfterDownload(newItems);
        if (result.getResult().size() > 0) {
            adapter.setItems(newItems);
            adapter.notifyDataSetChanged();
        }
        if (adapter.isEmpty() && !environment.getConfig().getCourseDiscoveryConfig().isCourseDiscoveryEnabled()) {
            errorNotification.showError(R.string.no_courses_to_display, FontAwesomeIcons.fa_exclamation_circle, 0, null);
            binding.myCourseList.setVisibility(View.GONE);
        } else {
            binding.myCourseList.setVisibility(View.VISIBLE);
            errorNotification.hideError();
        }
    }
    binding.swipeContainer.setRefreshing(false);
    binding.loadingIndicator.getRoot().setVisibility(View.GONE);
    if (!EventBus.getDefault().isRegistered(MyCoursesListFragment.this)) {
        EventBus.getDefault().registerSticky(MyCoursesListFragment.this);
    }
}
Also used : EnrolledCoursesResponse(org.edx.mobile.model.api.EnrolledCoursesResponse) ArrayList(java.util.ArrayList) AuthException(org.edx.mobile.exception.AuthException) HttpStatusException(org.edx.mobile.http.HttpStatusException) View(android.view.View) HttpStatusException(org.edx.mobile.http.HttpStatusException) AuthException(org.edx.mobile.exception.AuthException)

Example 4 with HttpStatusException

use of org.edx.mobile.http.HttpStatusException in project edx-app-android by edx.

the class ErrorHandlingOkCallback method onResponse.

/**
 * The original callback method invoked by OkHttp upon receiving an HTTP response. This method
 * definition provides extra information that's not needed by most individual callback
 * implementations, and is also invoked when HTTP error status codes are encountered (forcing
 * the implementation to manually check for success in each case). Therefore this implementation
 * delegates to {@link #onResponse(T)} in the case where it receives a successful HTTP status
 * code, and to {@link #onFailure(Throwable)} otherwise, passing an instance of
 * {@link HttpStatusException} with the relevant error status code. This method is declared as
 * final, as subclasses are meant to be implementing the abstract {@link #onResponse(T)} method
 * instead of this one.
 * <p>
 * This implementation takes care of delivering the appropriate error message to it's registered
 * callback, and invoking the callback for request process completion.
 *
 * @param call The Call object that was used to enqueue the request.
 * @param response The HTTP response data.
 */
@Override
public final void onResponse(@NonNull Call call, @NonNull final Response response) {
    if (!response.isSuccessful()) {
        deliverFailure(new HttpStatusException(response));
    } else {
        final String responseBodyString;
        try {
            responseBodyString = response.body().string();
        } catch (IOException error) {
            deliverFailure(error);
            return;
        }
        final T responseBody = gson.fromJson(responseBodyString, responseBodyType);
        handler.post(new Runnable() {

            @Override
            public void run() {
                if (progressCallback != null) {
                    progressCallback.finishProcess();
                }
                onResponse(responseBody);
                // Show SnackBar if user is seeing cached content while being offline.
                if (response.networkResponse() == null && !NetworkUtil.isConnected(context)) {
                    if (snackbarErrorNotification != null && refreshListener != null) {
                        snackbarErrorNotification.showError(R.string.offline_text, FontAwesomeIcons.fa_wifi, R.string.lbl_reload, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                if (NetworkUtil.isConnected(context)) {
                                    refreshListener.onRefresh();
                                    snackbarErrorNotification.hideError();
                                }
                            }
                        });
                    }
                }
                onFinish();
            }
        });
    }
}
Also used : HttpStatusException(org.edx.mobile.http.HttpStatusException) IOException(java.io.IOException) View(android.view.View)

Example 5 with HttpStatusException

use of org.edx.mobile.http.HttpStatusException in project edx-app-android by edx.

the class OkCallback method onResponse.

/**
 * The original callback method invoked by OkHttp upon receiving an HTTP response. This method
 * definition provides extra information that's not needed by most individual callback
 * implementations, and is also invoked when HTTP error status codes are encountered (forcing
 * the implementation to manually check for success in each case). Therefore this implementation
 * only delegates to {@link #onResponse(T)} in the case where it receives a successful HTTP
 * status code, and to {@link #onFailure(Throwable)} otherwise, passing an instance of
 * {@link HttpStatusException} with the relevant error status code. This method is declared as
 * final, as subclasses are meant to be implementing the abstract {@link #onResponse(T)} method
 * instead of this one.
 *
 * @param call The Call object that was used to enqueue the request.
 * @param response The HTTP response data.
 */
@Override
public final void onResponse(@NonNull final Call call, @NonNull final Response response) {
    if (response.isSuccessful()) {
        final String responseBodyString;
        try {
            responseBodyString = response.body().string();
        } catch (IOException error) {
            onFailure(error);
            return;
        }
        final T responseBody = gson.fromJson(responseBodyString, responseBodyType);
        handler.post(new Runnable() {

            @Override
            public void run() {
                onResponse(responseBody);
            }
        });
    } else {
        onFailure(new HttpStatusException(response));
    }
}
Also used : HttpStatusException(org.edx.mobile.http.HttpStatusException) IOException(java.io.IOException)

Aggregations

HttpStatusException (org.edx.mobile.http.HttpStatusException)12 View (android.view.View)4 IOException (java.io.IOException)3 AuthResponse (org.edx.mobile.authentication.AuthResponse)3 DialogInterface (android.content.DialogInterface)2 ArrayList (java.util.ArrayList)2 Request (okhttp3.Request)2 AuthException (org.edx.mobile.exception.AuthException)2 LoginErrorMessage (org.edx.mobile.exception.LoginErrorMessage)2 LoginException (org.edx.mobile.exception.LoginException)2 EnrolledCoursesResponse (org.edx.mobile.model.api.EnrolledCoursesResponse)2 FormFieldMessageBody (org.edx.mobile.model.api.FormFieldMessageBody)2 Bundle (android.os.Bundle)1 NonNull (android.support.annotation.NonNull)1 OnClickListener (android.view.View.OnClickListener)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 HashMap (java.util.HashMap)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 Response (okhttp3.Response)1