use of org.edx.mobile.model.api.FormFieldMessageBody in project edx-app-android by edx.
the class LoginAPI method register.
@NonNull
private void register(Bundle parameters) throws Exception {
final Map<String, String> parameterMap = new HashMap<>();
for (String key : parameters.keySet()) {
parameterMap.put(key, parameters.getString(key));
}
Response<ResponseBody> response = loginService.register(parameterMap).execute();
if (!response.isSuccessful()) {
final int errorCode = response.code();
final String errorBody = response.errorBody().string();
if ((errorCode == HttpStatus.BAD_REQUEST || errorCode == HttpStatus.CONFLICT) && !android.text.TextUtils.isEmpty(errorBody)) {
try {
final FormFieldMessageBody body = gson.fromJson(errorBody, FormFieldMessageBody.class);
if (body != null && body.size() > 0) {
throw new RegistrationException(body);
}
} catch (JsonSyntaxException ex) {
// Looks like the response does not contain form validation errors.
}
}
throw new HttpStatusException(response);
}
}
use of org.edx.mobile.model.api.FormFieldMessageBody in project edx-app-android by edx.
the class RegisterActivity method createAccount.
private void createAccount() {
boolean hasError = false;
// prepare query (POST body)
Bundle parameters = new Bundle();
for (IRegistrationFieldView v : mFieldViews) {
if (v.isValidInput()) {
if (v.hasValue()) {
// we submit the field only if it provides a value
parameters.putString(v.getField().getName(), v.getCurrentValue().getAsString());
}
} else {
if (!hasError) {
// this is the first input field with error,
// so focus on it after showing the popup
showErrorPopup(v.getOnErrorFocusView());
}
hasError = true;
}
}
// set honor_code and terms_of_service to true
parameters.putString("honor_code", "true");
parameters.putString("terms_of_service", "true");
// set parameter required by social registration
final String access_token = loginPrefs.getSocialLoginAccessToken();
final String provider = loginPrefs.getSocialLoginProvider();
boolean fromSocialNet = !TextUtils.isEmpty(access_token);
if (fromSocialNet) {
parameters.putString("access_token", access_token);
parameters.putString("provider", provider);
parameters.putString("client_id", environment.getConfig().getOAuthClientId());
}
// do NOT proceed if validations are failed
if (hasError) {
return;
}
// Send analytics event for Create Account button click
final String appVersion = String.format("%s v%s", getString(R.string.android), BuildConfig.VERSION_NAME);
environment.getAnalyticsRegistry().trackCreateAccountClicked(appVersion, provider);
showProgress();
final SocialFactory.SOCIAL_SOURCE_TYPE backsourceType = SocialFactory.SOCIAL_SOURCE_TYPE.fromString(provider);
final RegisterTask task = new RegisterTask(this, parameters, access_token, backsourceType) {
@Override
public void onSuccess(AuthResponse auth) {
environment.getAnalyticsRegistry().trackRegistrationSuccess(appVersion, provider);
onUserLoginSuccess(auth.profile);
}
@Override
public void onException(Exception ex) {
hideProgress();
if (ex instanceof LoginAPI.RegistrationException) {
final FormFieldMessageBody messageBody = ((LoginAPI.RegistrationException) ex).getFormErrorBody();
boolean errorShown = false;
for (String key : messageBody.keySet()) {
if (key == null)
continue;
for (IRegistrationFieldView fieldView : mFieldViews) {
if (key.equalsIgnoreCase(fieldView.getField().getName())) {
List<RegisterResponseFieldError> error = messageBody.get(key);
showErrorOnField(error, fieldView);
if (!errorShown) {
// this is the first input field with error,
// so focus on it after showing the popup
showErrorPopup(fieldView.getOnErrorFocusView());
errorShown = true;
}
break;
}
}
}
if (errorShown) {
// Return here to avoid falling back to the generic error handler.
return;
}
}
// If app version is un-supported
if (ex instanceof HttpStatusException && ((HttpStatusException) ex).getStatusCode() == HttpStatus.UPGRADE_REQUIRED) {
RegisterActivity.this.showAlertDialog(null, getString(R.string.app_version_unsupported_register_msg), getString(R.string.label_update), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AppStoreUtils.openAppInAppStore(RegisterActivity.this);
}
}, getString(android.R.string.cancel), null);
} else {
RegisterActivity.this.showAlertDialog(null, ErrorUtils.getErrorMessage(ex, RegisterActivity.this));
}
}
};
task.execute();
}
Aggregations