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();
}
}
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);
}
}
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);
}
}
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();
}
});
}
}
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));
}
}
Aggregations