use of com.android.volley.VolleyError in project WordPress-Android by wordpress-mobile.
the class PeopleUtils method sendInvitations.
public static void sendInvitations(final List<String> usernames, Role role, String message, long dotComBlogId, final InvitationsSendCallback callback) {
com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {
@Override
public void onResponse(JSONObject jsonObject) {
if (callback == null) {
return;
}
if (jsonObject == null) {
callback.onError();
return;
}
Map<String, String> failedUsernames = new LinkedHashMap<>();
JSONObject errors = jsonObject.optJSONObject("errors");
if (errors != null) {
for (String username : usernames) {
JSONObject userError = errors.optJSONObject(username);
if (userError != null) {
failedUsernames.put(username, userError.optString("message"));
}
}
}
List<String> succeededUsernames = new ArrayList<>();
JSONArray succeededUsernamesJson = jsonObject.optJSONArray("sent");
if (succeededUsernamesJson == null) {
callback.onError();
return;
}
for (int i = 0; i < succeededUsernamesJson.length(); i++) {
String username = succeededUsernamesJson.optString(i);
if (usernames.contains(username)) {
succeededUsernames.add(username);
}
}
if (failedUsernames.size() + succeededUsernames.size() != usernames.size()) {
callback.onError();
}
callback.onSent(succeededUsernames, failedUsernames);
}
};
RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
AppLog.e(AppLog.T.API, volleyError);
if (callback != null) {
callback.onError();
}
}
};
String path = String.format(Locale.US, "sites/%s/invites/new", dotComBlogId);
Map<String, String> params = new HashMap<>();
for (String username : usernames) {
// specify an array key so to make the map key unique
params.put("invitees[" + username + "]", username);
}
params.put("role", role.toRESTString());
params.put("message", message);
WordPress.getRestClientUtilsV1_1().post(path, params, null, listener, errorListener);
}
use of com.android.volley.VolleyError in project WordPress-Android by wordpress-mobile.
the class PeopleUtils method fetchViewers.
public static void fetchViewers(final SiteModel site, final int offset, final FetchViewersCallback callback) {
com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {
@Override
public void onResponse(JSONObject jsonObject) {
if (jsonObject != null && callback != null) {
try {
JSONArray jsonArray = jsonObject.getJSONArray("viewers");
List<Person> people = peopleListFromJSON(jsonArray, site.getId(), Person.PersonType.VIEWER);
int numberOfUsers = jsonObject.optInt("found");
boolean isEndOfList = (people.size() + offset) >= numberOfUsers;
callback.onSuccess(people, isEndOfList);
} catch (JSONException e) {
AppLog.e(T.API, "JSON exception occurred while parsing the response for " + "sites/%s/viewers: " + e);
callback.onError();
}
}
}
};
RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
AppLog.e(T.API, volleyError);
if (callback != null) {
callback.onError();
}
}
};
int page = (offset / FETCH_LIMIT) + 1;
Map<String, String> params = new HashMap<>();
params.put("number", Integer.toString(FETCH_LIMIT));
params.put("page", Integer.toString(page));
String path = String.format(Locale.US, "sites/%d/viewers", site.getSiteId());
WordPress.getRestClientUtilsV1_1().get(path, params, null, listener, errorListener);
}
use of com.android.volley.VolleyError in project WordPress-Android by wordpress-mobile.
the class PeopleUtils method updateRole.
public static void updateRole(final SiteModel site, long personID, Role newRole, final int localTableBlogId, final UpdateUserCallback callback) {
com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() {
@Override
public void onResponse(JSONObject jsonObject) {
if (jsonObject != null && callback != null) {
try {
Person person = Person.userFromJSON(jsonObject, localTableBlogId);
if (person != null) {
callback.onSuccess(person);
} else {
AppLog.e(T.API, "Couldn't map jsonObject + " + jsonObject + " to person model.");
callback.onError();
}
} catch (JSONException e) {
callback.onError();
}
}
}
};
RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
AppLog.e(T.API, volleyError);
if (callback != null) {
callback.onError();
}
}
};
Map<String, String> params = new HashMap<>();
params.put("roles", newRole.toRESTString());
String path = String.format(Locale.US, "sites/%d/users/%d", site.getSiteId(), personID);
WordPress.getRestClientUtilsV1_1().post(path, params, null, listener, errorListener);
}
use of com.android.volley.VolleyError in project WordPress-Android by wordpress-mobile.
the class SiteSettingsFragment method requestPurchasesForDeletionCheck.
private void requestPurchasesForDeletionCheck() {
final ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "", getString(R.string.checking_purchases), true, false);
AnalyticsUtils.trackWithSiteDetails(AnalyticsTracker.Stat.SITE_SETTINGS_DELETE_SITE_PURCHASES_REQUESTED, mSite);
WordPress.getRestClientUtils().getSitePurchases(mSite.getSiteId(), new RestRequest.Listener() {
@Override
public void onResponse(JSONObject response) {
dismissProgressDialog(progressDialog);
if (isAdded()) {
showPurchasesOrDeleteSiteDialog(response, mSite);
}
}
}, new RestRequest.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
dismissProgressDialog(progressDialog);
if (isAdded()) {
ToastUtils.showToast(getActivity(), getString(R.string.purchases_request_error));
AppLog.e(AppLog.T.API, "Error occurred while requesting purchases for deletion check: " + error.toString());
}
}
});
}
use of com.android.volley.VolleyError in project WordPress-Android by wordpress-mobile.
the class SiteSettingsFragment method exportSite.
private void exportSite() {
if (mSite.isWPCom()) {
final ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "", getActivity().getString(R.string.exporting_content_progress), true, true);
WordPress.getRestClientUtils().exportContentAll(mSite.getSiteId(), new RestRequest.Listener() {
@Override
public void onResponse(JSONObject response) {
if (isAdded()) {
AnalyticsUtils.trackWithSiteDetails(AnalyticsTracker.Stat.SITE_SETTINGS_EXPORT_SITE_RESPONSE_OK, mSite);
dismissProgressDialog(progressDialog);
Snackbar.make(getView(), R.string.export_email_sent, Snackbar.LENGTH_LONG).show();
}
}
}, new RestRequest.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (isAdded()) {
HashMap<String, Object> errorProperty = new HashMap<>();
errorProperty.put(ANALYTICS_ERROR_PROPERTY_KEY, error.getMessage());
AnalyticsUtils.trackWithSiteDetails(AnalyticsTracker.Stat.SITE_SETTINGS_EXPORT_SITE_RESPONSE_ERROR, mSite, errorProperty);
dismissProgressDialog(progressDialog);
}
}
});
}
}
Aggregations