Search in sources :

Example 56 with VolleyError

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);
}
Also used : VolleyError(com.android.volley.VolleyError) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) LinkedHashMap(java.util.LinkedHashMap) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject)

Example 57 with VolleyError

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);
}
Also used : VolleyError(com.android.volley.VolleyError) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) Person(org.wordpress.android.models.Person)

Example 58 with VolleyError

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);
}
Also used : VolleyError(com.android.volley.VolleyError) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JSONException(org.json.JSONException) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) Person(org.wordpress.android.models.Person)

Example 59 with VolleyError

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());
            }
        }
    });
}
Also used : VolleyError(com.android.volley.VolleyError) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) ProgressDialog(android.app.ProgressDialog)

Example 60 with VolleyError

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);
                }
            }
        });
    }
}
Also used : VolleyError(com.android.volley.VolleyError) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) ProgressDialog(android.app.ProgressDialog)

Aggregations

VolleyError (com.android.volley.VolleyError)180 Response (com.android.volley.Response)121 JSONObject (org.json.JSONObject)104 HashMap (java.util.HashMap)70 JSONException (org.json.JSONException)66 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)61 RequestQueue (com.android.volley.RequestQueue)57 User (model.User)49 JSONArray (org.json.JSONArray)35 CustomRequestArray (dz.easy.androidclient.Util.CustomRequestArray)18 Context (android.content.Context)16 Toast (android.widget.Toast)16 StringRequest (com.android.volley.toolbox.StringRequest)16 RestRequest (com.wordpress.rest.RestRequest)16 CustomRequest (dz.easy.androidclient.Util.CustomRequest)12 TextView (android.widget.TextView)11 MockHttpStack (com.android.volley.mock.MockHttpStack)9 GsonRequest (com.android.volley.toolbox.GsonRequest)9 ImageContainer (com.android.volley.toolbox.ImageLoader.ImageContainer)9 Test (org.junit.Test)9