Example usage for org.json JSONObject optInt

List of usage examples for org.json JSONObject optInt

Introduction

In this page you can find the example usage for org.json JSONObject optInt.

Prototype

public int optInt(String key) 

Source Link

Document

Get an optional int value associated with a key, or zero if there is no such key or if the value is not a number.

Usage

From source file:com.vk.sdkweb.api.model.VKApiPoll.java

/**
 * Fills a Poll instance from JSONObject.
 *//*from ww  w .  j av  a 2s  . c  om*/
public VKApiPoll parse(JSONObject source) {
    id = source.optInt("id");
    owner_id = source.optInt("owner_id");
    created = source.optLong("created");
    question = source.optString("question");
    votes = source.optInt("votes");
    answer_id = source.optInt("answer_id");
    answers = new VKList<Answer>(source.optJSONArray("answers"), Answer.class);
    return this;
}

From source file:org.catnut.fragment.MyRelationshipFragment.java

@Override
protected void refresh() {
    // ???// www.ja v  a 2  s . c om
    if (!isNetworkAvailable()) {
        Toast.makeText(getActivity(), getString(R.string.network_unavailable), Toast.LENGTH_SHORT).show();
        initFromLocal();
        return;
    }
    // refresh!
    CatnutAPI api = mIsFollowing ? FriendshipsAPI.friends(mUid, getFetchSize(), 0, 1)
            : FriendshipsAPI.followers(mUid, getFetchSize(), 0, 1);
    mRequestQueue.add(new CatnutRequest(getActivity(), api, new UserProcessor.UsersProcessor(),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "refresh done...");
                    mTotal = response.optInt(TOTAL_NUMBER);
                    mNextCursor = response.optInt(NEXT_CURSOR);
                    // ???
                    mLastTotalNumber = 0;
                    JSONArray jsonArray = response.optJSONArray(User.MULTIPLE);
                    int newSize = jsonArray.length(); // ...
                    Bundle args = new Bundle();
                    args.putInt(TAG, newSize);
                    getLoaderManager().restartLoader(0, args, MyRelationshipFragment.this);
                }
            }, errorListener)).setTag(TAG);
}

From source file:org.catnut.fragment.MyRelationshipFragment.java

private void loadFromCloud(long max_id) {
    mSwipeRefreshLayout.setRefreshing(true);
    CatnutAPI api = mIsFollowing ? FriendshipsAPI.friends(mUid, getFetchSize(), mNextCursor, 1)
            : FriendshipsAPI.followers(mUid, getFetchSize(), mNextCursor, 1);
    mRequestQueue.add(new CatnutRequest(getActivity(), api, new UserProcessor.UsersProcessor(),
            new Response.Listener<JSONObject>() {
                @Override//from w  w  w .  java 2  s . c  o  m
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "load more from cloud done...");
                    mTotal = response.optInt(TOTAL_NUMBER);
                    mNextCursor = response.optInt(NEXT_CURSOR);
                    mLastTotalNumber = mAdapter.getCount();
                    int newSize = response.optJSONArray(User.MULTIPLE).length() + mAdapter.getCount();
                    Bundle args = new Bundle();
                    args.putInt(TAG, newSize);
                    getLoaderManager().restartLoader(0, args, MyRelationshipFragment.this);
                }
            }, errorListener)).setTag(TAG);
}

From source file:org.chromium.ChromeNotifications.java

private void makeNotification(final CordovaArgs args) throws JSONException {
    String notificationId = args.getString(0);
    JSONObject options = args.getJSONObject(1);
    Resources resources = cordova.getActivity().getResources();
    Bitmap largeIcon = makeBitmap(options.getString("iconUrl"),
            resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
            resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height));
    int smallIconId = resources.getIdentifier("notification_icon", "drawable",
            cordova.getActivity().getPackageName());
    if (smallIconId == 0) {
        smallIconId = resources.getIdentifier("icon", "drawable", cordova.getActivity().getPackageName());
    }//from ww  w .  ja v a  2s.  c om
    NotificationCompat.Builder builder = new NotificationCompat.Builder(cordova.getActivity())
            .setSmallIcon(smallIconId).setContentTitle(options.getString("title"))
            .setContentText(options.getString("message")).setLargeIcon(largeIcon)
            .setPriority(options.optInt("priority"))
            .setContentIntent(makePendingIntent(NOTIFICATION_CLICKED_ACTION, notificationId, -1,
                    PendingIntent.FLAG_CANCEL_CURRENT))
            .setDeleteIntent(makePendingIntent(NOTIFICATION_CLOSED_ACTION, notificationId, -1,
                    PendingIntent.FLAG_CANCEL_CURRENT));
    double eventTime = options.optDouble("eventTime");
    if (eventTime != 0) {
        builder.setWhen(Math.round(eventTime));
    }
    JSONArray buttons = options.optJSONArray("buttons");
    if (buttons != null) {
        for (int i = 0; i < buttons.length(); i++) {
            JSONObject button = buttons.getJSONObject(i);
            builder.addAction(android.R.drawable.ic_dialog_info, button.getString("title"), makePendingIntent(
                    NOTIFICATION_BUTTON_CLICKED_ACTION, notificationId, i, PendingIntent.FLAG_CANCEL_CURRENT));
        }
    }
    String type = options.getString("type");
    Notification notification;
    if ("image".equals(type)) {
        NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle(builder);
        String bigImageUrl = options.optString("imageUrl");
        if (!bigImageUrl.isEmpty()) {
            bigPictureStyle.bigPicture(makeBitmap(bigImageUrl, 0, 0));
        }
        notification = bigPictureStyle.build();
    } else if ("list".equals(type)) {
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(builder);
        JSONArray items = options.optJSONArray("items");
        if (items != null) {
            for (int i = 0; i < items.length(); i++) {
                JSONObject item = items.getJSONObject(i);
                inboxStyle.addLine(Html.fromHtml("<b>" + item.getString("title")
                        + "</b>&nbsp;&nbsp;&nbsp;&nbsp;" + item.getString("message")));
            }
        }
        notification = inboxStyle.build();
    } else {
        if ("progress".equals(type)) {
            int progress = options.optInt("progress");
            builder.setProgress(100, progress, false);
        }
        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(builder);
        bigTextStyle.bigText(options.getString("message"));
        notification = bigTextStyle.build();
    }
    notificationManager.notify(notificationId.hashCode(), notification);
}

From source file:com.vk.sdkweb.api.model.VKApiPhotoAlbum.java

/**
 * Creates a PhotoAlbum instance from JSONObject.
 *///from   w w w.j a v  a  2s . co  m
public VKApiPhotoAlbum parse(JSONObject from) {
    id = from.optInt("id");
    thumb_id = from.optInt("thumb_id");
    owner_id = from.optInt("owner_id");
    title = from.optString("title");
    description = from.optString("description");
    created = from.optLong("created");
    updated = from.optLong("updated");
    size = from.optInt("size");
    can_upload = ParseUtils.parseBoolean(from, "can_upload");
    thumb_src = from.optString("thumb_src");
    if (from.has("privacy")) {
        privacy = from.optInt("privacy");
    } else {
        privacy = VKPrivacy.parsePrivacy(from.optJSONObject("privacy_view"));
    }
    JSONArray sizes = from.optJSONArray("sizes");
    if (sizes != null) {
        photo.fill(sizes);
    } else {
        photo.add(VKApiPhotoSize.create(COVER_S, 75, 55));
        photo.add(VKApiPhotoSize.create(COVER_M, 130, 97));
        photo.add(VKApiPhotoSize.create(COVER_X, 432, 249));
        photo.sort();
    }
    return this;
}

From source file:com.vk.sdkweb.api.model.VKApiCommunityFull.java

public VKApiCommunityFull parse(JSONObject jo) {
    super.parse(jo);

    JSONObject city = jo.optJSONObject(CITY);
    if (city != null) {
        this.city = new VKApiCity().parse(city);
    }// w  w w . j  a va2 s .  com
    JSONObject country = jo.optJSONObject(COUNTRY);
    if (country != null) {
        this.country = new VKApiCountry().parse(country);
    }

    JSONObject place = jo.optJSONObject(PLACE);
    if (place != null)
        this.place = new VKApiPlace().parse(place);

    description = jo.optString(DESCRIPTION);
    wiki_page = jo.optString(WIKI_PAGE);
    members_count = jo.optInt(MEMBERS_COUNT);

    JSONObject counters = jo.optJSONObject(COUNTERS);
    if (counters != null)
        this.counters = new Counters(place);

    start_date = jo.optLong(START_DATE);
    end_date = jo.optLong(END_DATE);
    can_post = ParseUtils.parseBoolean(jo, CAN_POST);
    can_see_all_posts = ParseUtils.parseBoolean(jo, CAN_SEE_ALL_POSTS);
    status = jo.optString(STATUS);

    JSONObject status_audio = jo.optJSONObject("status_audio");
    if (status_audio != null)
        this.status_audio = new VKApiAudio().parse(status_audio);

    contacts = new VKList<Contact>(jo.optJSONArray(CONTACTS), Contact.class);
    links = new VKList<Link>(jo.optJSONArray(LINKS), Link.class);
    fixed_post = jo.optInt(FIXED_POST);
    verified = ParseUtils.parseBoolean(jo, VERIFIED);
    blacklisted = ParseUtils.parseBoolean(jo, VERIFIED);
    site = jo.optString(SITE);
    return this;
}

From source file:com.vk.sdkweb.api.model.VKApiDocument.java

/**
 * Fills a Doc instance from JSONObject.
 *///from   w w  w.  ja v a  2  s.c o  m
public VKApiDocument parse(JSONObject jo) {
    id = jo.optInt("id");
    owner_id = jo.optInt("owner_id");
    title = jo.optString("title");
    size = jo.optLong("size");
    ext = jo.optString("ext");
    url = jo.optString("url");
    access_key = jo.optString("access_key");

    photo_100 = jo.optString("photo_100");
    if (!TextUtils.isEmpty(photo_100)) {
        photo.add(VKApiPhotoSize.create(photo_100, 100, 75));
    }
    photo_130 = jo.optString("photo_130");
    if (!TextUtils.isEmpty(photo_130)) {
        photo.add(VKApiPhotoSize.create(photo_130, 130, 100));
    }
    photo.sort();
    return this;
}

From source file:com.melniqw.instagramsdk.Video.java

public static Video fromJSON(JSONObject o) throws JSONException {
    if (o == null)
        return null;
    Video video = new Video();
    JSONObject lowResolutionJSON = o.optJSONObject("low_resolution");
    video.lowResolution.url = lowResolutionJSON.optString("url");
    video.lowResolution.width = lowResolutionJSON.optInt("width");
    video.lowResolution.height = lowResolutionJSON.optInt("height");
    JSONObject standartResolutionJSON = o.optJSONObject("standard_resolution");
    video.standartResolution.url = standartResolutionJSON.optString("url");
    video.standartResolution.width = standartResolutionJSON.optInt("width");
    video.standartResolution.height = standartResolutionJSON.optInt("height");
    JSONObject lowBandwidthJSON = o.optJSONObject("low_bandwidth");
    video.lowBandwidth.url = lowBandwidthJSON.optString("url");
    video.lowBandwidth.width = lowBandwidthJSON.optInt("width");
    video.lowBandwidth.height = lowBandwidthJSON.optInt("height");
    return video;
}

From source file:se.chalmers.watchme.utils.MovieHelper.java

/**
 * Convert a JSONArray of Movies to a list of Movies
 * /*from ww w . ja va  2s  .  co m*/
 * <p>Each Movie object is initialized with the attribute
 * <code>original_name</code> from the input array. The 
 * attribute <code>imdb_id</code> is also set on the movie.</p>
 * 
 * @param input The JSONArray of movies as JSONObjects
 * @return A List of Movies
 */
public static List<Movie> jsonArrayToMovieList(JSONArray input) {
    List<Movie> list = new ArrayList<Movie>();

    // Parse the JSON objects and add to list
    for (int i = 0; i < input.length(); i++) {
        JSONObject o = input.optJSONObject(i);

        Movie movie = new Movie(o.optString(Movie.JSON_KEY_NAME));
        // Don't forget the ID
        movie.setApiID(o.optInt(Movie.JSON_KEY_ID));
        list.add(movie);
    }

    return list;
}

From source file:com.chaosinmotion.securechat.server.commands.SendMessages.java

public static ReturnResult processRequest(Login.UserInfo userinfo, JSONObject requestParams)
        throws ClassNotFoundException, SQLException, IOException {
    int messageid = 0;

    JSONArray array = requestParams.optJSONArray("messages");
    int i, len = array.length();
    for (i = 0; i < len; ++i) {
        JSONObject mrecord = array.getJSONObject(i);
        String checksum = mrecord.optString("checksum");
        String message = mrecord.optString("message");
        String deviceid = mrecord.optString("deviceid");
        int destuser = mrecord.optInt("destuser");

        byte[] mdata = Base64.decode(message);

        /*//from   ww  w  .  ja  va 2s . c om
         * If destuser is not provided, then this indicates that the
         * current logged in user is sending a message *to* the device's
         * owner. The 'toflag' is set, meaning the device is owned by
         * the user in userinfo, the message is being sent to the
         * destuser.
         */
        if (destuser == 0) {
            MessageQueue.getInstance().enqueue(userinfo.getUserID(), deviceid, false, mdata, checksum);
        } else {
            messageid = MessageQueue.getInstance().enqueue(destuser, deviceid, true, mdata, checksum);
        }
    }
    return new SimpleReturnResult("messageid", messageid);
}