Search in sources :

Example 51 with VolleyError

use of com.android.volley.VolleyError in project WordPress-Android by wordpress-mobile.

the class NotificationsUtils method unregisterDevicePushNotifications.

public static void unregisterDevicePushNotifications(final Context ctx) {
    RestRequest.Listener listener = new RestRequest.Listener() {

        @Override
        public void onResponse(JSONObject jsonObject) {
            AppLog.d(T.NOTIFS, "Unregister token action succeeded");
            SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
            editor.remove(WPCOM_PUSH_DEVICE_SERVER_ID);
            editor.remove(WPCOM_PUSH_DEVICE_UUID);
            editor.remove(WPCOM_PUSH_DEVICE_TOKEN);
            editor.apply();
        }
    };
    RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            AppLog.e(T.NOTIFS, "Unregister token action failed", volleyError);
        }
    };
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx);
    String deviceID = settings.getString(WPCOM_PUSH_DEVICE_SERVER_ID, null);
    if (TextUtils.isEmpty(deviceID)) {
        return;
    }
    WordPress.getRestClientUtils().post("/devices/" + deviceID + "/delete", listener, errorListener);
}
Also used : VolleyError(com.android.volley.VolleyError) RestRequest(com.wordpress.rest.RestRequest) JSONObject(org.json.JSONObject) SharedPreferences(android.content.SharedPreferences)

Example 52 with VolleyError

use of com.android.volley.VolleyError in project WordPress-Android by wordpress-mobile.

the class AztecImageLoader method loadImage.

@Override
public void loadImage(String url, final Callbacks callbacks, int maxWidth) {
    // TODO: if a local file then load it directly. This is a quick fix though.
    if (new File(url).exists()) {
        Bitmap bitmap = BitmapFactory.decodeFile(url);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
        callbacks.onImageLoaded(bitmapDrawable);
        return;
    }
    WordPress.sImageLoader.get(url, new ImageLoader.ImageListener() {

        @Override
        public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
            Bitmap bitmap = response.getBitmap();
            if (bitmap == null) {
                // the loader tries to let us know to just use the default image for now
                callbacks.onUseDefaultImage();
            } else {
                BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), response.getBitmap());
                callbacks.onImageLoaded(bitmapDrawable);
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            callbacks.onImageLoadingFailed();
        }
    }, maxWidth, 0);
}
Also used : VolleyError(com.android.volley.VolleyError) Bitmap(android.graphics.Bitmap) BitmapDrawable(android.graphics.drawable.BitmapDrawable) ImageLoader(com.android.volley.toolbox.ImageLoader) File(java.io.File)

Example 53 with VolleyError

use of com.android.volley.VolleyError in project WordPress-Android by wordpress-mobile.

the class PeopleUtils method fetchFollowers.

private static void fetchFollowers(final SiteModel site, final int page, final FetchFollowersCallback callback, final boolean isEmailFollower) {
    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("subscribers");
                    Person.PersonType personType = isEmailFollower ? Person.PersonType.EMAIL_FOLLOWER : Person.PersonType.FOLLOWER;
                    List<Person> people = peopleListFromJSON(jsonArray, site.getId(), personType);
                    int pageFetched = jsonObject.optInt("page");
                    int numberOfPages = jsonObject.optInt("pages");
                    boolean isEndOfList = page >= numberOfPages || page >= FOLLOWER_PAGE_LIMIT;
                    callback.onSuccess(people, pageFetched, isEndOfList);
                } catch (JSONException e) {
                    AppLog.e(T.API, "JSON exception occurred while parsing the response for " + "sites/%s/stats/followers: " + 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("max", Integer.toString(FETCH_LIMIT));
    params.put("page", Integer.toString(page));
    params.put("type", isEmailFollower ? "email" : "wp_com");
    String path = String.format(Locale.US, "sites/%d/stats/followers", 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 54 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 55 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)

Aggregations

VolleyError (com.android.volley.VolleyError)181 Response (com.android.volley.Response)122 JSONObject (org.json.JSONObject)105 HashMap (java.util.HashMap)71 JSONException (org.json.JSONException)67 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)62 RequestQueue (com.android.volley.RequestQueue)58 User (model.User)50 JSONArray (org.json.JSONArray)35 CustomRequestArray (dz.easy.androidclient.Util.CustomRequestArray)18 Context (android.content.Context)17 Toast (android.widget.Toast)17 StringRequest (com.android.volley.toolbox.StringRequest)16 RestRequest (com.wordpress.rest.RestRequest)16 TextView (android.widget.TextView)12 CustomRequest (dz.easy.androidclient.Util.CustomRequest)12 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