Example usage for org.json JSONObject has

List of usage examples for org.json JSONObject has

Introduction

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

Prototype

public boolean has(String key) 

Source Link

Document

Determine if the JSONObject contains a specific key.

Usage

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve a boolean stored at the provided key from the provided JSON object
 * @param obj The JSON object to retrieve from
 * @param key The key to retrieve//from  w w w. j av a2s . co  m
 * @return the boolean stored in the key, or the default value if the key doesn't exist
 */
public static boolean safeGetBoolean(JSONObject obj, String key, boolean defaultValue) {
    if (obj == null || TextUtils.isEmpty(key))
        return defaultValue;
    if (obj.has(key)) {
        try {
            return obj.getBoolean(key);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get boolean from key " + key, e);
        }
    }
    return defaultValue;
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve a double stored at the provided key from the provided JSON object
 * @param obj The JSON object to retrieve from
 * @param key The key to retrieve//from  w w  w  .java  2 s. co  m
 * @return the double stored in the key, or the default value if the key doesn't exist
 */
public static double safeGetDouble(JSONObject obj, String key, double defaultValue) {
    if (obj == null || TextUtils.isEmpty(key))
        return defaultValue;
    if (obj.has(key)) {
        try {
            return obj.getDouble(key);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get double from key " + key, e);
        }
    }
    return defaultValue;
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve an integer stored at the provided key from the provided JSON object
 * @param obj The JSON object to retrieve from
 * @param key The key to retrieve//from  www  .  ja va  2s.  c o m
 * @return the integer stored in the key, or the default value if the key doesn't exist
 */
public static int safeGetInt(JSONObject obj, String key, int defaultValue) {
    if (obj == null || TextUtils.isEmpty(key))
        return defaultValue;
    if (obj.has(key)) {
        try {
            return obj.getInt(key);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get int from key " + key, e);
        }
    }
    return defaultValue;
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve the JSON object stored at the provided key from the provided JSON object
 * @param obj The JSON object to retrieve from
 * @param key The key to retrieve//from   w  w  w  .  j  av  a 2s  .  co m
 * @return the JSON object stored in the key, or null if the key doesn't exist
 */
public static JSONObject safeGetJSONObject(JSONObject obj, String key) {
    if (obj == null || TextUtils.isEmpty(key))
        return null;
    if (obj.has(key)) {
        try {
            return obj.getJSONObject(key);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get JSON object from key " + key, e);
        }
    }
    return null;
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve a long stored at the provided key from the provided JSON object
 * @param obj The JSON object to retrieve from
 * @param key The key to retrieve/*from w  ww  .j a v  a 2  s  . co m*/
 * @return the long stored in the key, or the default value if the key doesn't exist
 */
public static long safeGetLong(JSONObject obj, String key, long defaultValue) {
    if (obj == null || TextUtils.isEmpty(key))
        return defaultValue;
    if (obj.has(key)) {
        try {
            return obj.getLong(key);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get long from key " + key, e);
        }
    }
    return defaultValue;
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve another JSON object stored at the provided key from the provided JSON object
 * @param obj The JSON object to retrieve from
 * @param key The key to retrieve/*w  w  w  . j a  va 2  s . co  m*/
 * @return the JSON object stored in the key, or null if the key doesn't exist
 */
public static JSONObject safeGetObject(JSONObject obj, String key) {
    if (obj == null || TextUtils.isEmpty(key))
        return null;
    if (obj.has(key)) {
        try {
            return obj.getJSONObject(key);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get JSONObject from key " + key, e);
        }
    }
    return null;
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve a string stored at the provided key from the provided JSON object
 * @param obj The JSON object to retrieve from
 * @param key The key to retrieve// www.  ja v  a 2 s. c  o  m
 * @return the string stored in the key, or null if the key doesn't exist
 */
public static String safeGetString(JSONObject obj, String key) {
    if (obj == null || TextUtils.isEmpty(key))
        return null;
    if (obj.has(key)) {
        try {
            return obj.getString(key);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get string from key " + key, e);
        }
    }
    return null;
}

From source file:com.basetechnology.s0.agentserver.field.ChoiceField.java

public static Field fromJson(SymbolTable symbolTable, JSONObject fieldJson) {
    String type = fieldJson.optString("type");
    if (type == null || !type.equals("choice_field"))
        return null;
    String name = fieldJson.has("name") ? fieldJson.optString("name") : null;
    String label = fieldJson.has("label") ? fieldJson.optString("label") : null;
    String description = fieldJson.has("description") ? fieldJson.optString("description") : null;
    String defaultValue = fieldJson.has("default_value") ? fieldJson.optString("default_value") : null;
    List<String> choices = new ArrayList<String>();
    if (fieldJson.has("choices")) {
        JSONArray choicesJson = fieldJson.optJSONArray("choices");
        int n = choicesJson.length();
        for (int i = 0; i < n; i++)
            choices.add(choicesJson.optString(i));
    }//from ww w  .j  av a  2  s  . c  o m
    int nominalWidth = fieldJson.has("nominal_width") ? fieldJson.optInt("nominal_width") : 0;
    String compute = fieldJson.has("compute") ? fieldJson.optString("compute") : null;
    return new ChoiceField(symbolTable, name, label, description, defaultValue, choices, nominalWidth, compute);
}

From source file:org.nikki.omegle.Omegle.java

/**
 * Opens a new omegle session. Note: This CONNECTS THE SESSION!
 * /*from  ww w .  jav a  2  s . c  o  m*/
 * @param mode
 *            The omegle chat mode
 * @param objs
 *            The objects. If mode = SPY_QUESTION, param 1 should be the
 *            question. Parameters are checked, so you may pass event
 *            listeners through here.
 * 
 * @return The newly created session
 * @throws OmegleException
 *             If an error occurred while attempting to open, always due to
 *             an IOException
 */
public OmegleSession openSession(OmegleMode mode, Object... objs) throws OmegleException {
    try {
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("rcs", "1");
        vars.put("firstevents", firstEvents ? "1" : "0");

        if (mode == OmegleMode.SPY) {
            vars.put("wantsspy", "1");
        } else if (mode == OmegleMode.SPY_QUESTION) {
            if (objs.length > 0) {
                if (objs[0] instanceof String) {
                    vars.put("ask", objs[0].toString());
                } else {
                    throw new OmegleException("The question MUST be passed as the first parameter!");
                }
            } else {
                throw new OmegleException("You cannot open a spy question session without a question!");
            }
        }

        URL url = new URL(OPEN_URL + "?" + HttpUtil.implode(vars));

        JSONObject resp = new JSONObject(HttpUtil.post(url, ""));

        if (!resp.has("clientID")) {
            throw new OmegleException("Omegle didn't return a client id!");
        }

        OmegleSession session = new OmegleSession(this, resp.getString("clientID"));

        for (Object obj : objs) {
            if (obj instanceof OmegleEventListener) {
                session.addListener((OmegleEventListener) obj);
            }
        }

        if (resp.has("events")) {
            session.parseEvents(resp.getJSONArray("events"));
        }

        synchronized (sessions) {
            sessions.add(session);
        }

        return session;
    } catch (IOException e) {
        throw new OmegleException(e);
    } catch (JSONException e) {
        throw new OmegleException(e);
    }
}

From source file:com.freshplanet.nativeExtensions.C2DMBroadcastReceiver.java

/**
 * Get the parameters from the message and create a notification from it.
 * @param context/*w  w  w .  j  av a2s.  c  o  m*/
 * @param intent
 */
public void handleMessage(Context context, Intent intent) {
    try {
        registerResources(context);
        extractColors(context);

        FREContext ctxt = C2DMExtension.context;

        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // icon is required for notification.
        // @see http://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar.html

        int icon = notificationIcon;
        long when = System.currentTimeMillis();

        // json string

        String parameters = intent.getStringExtra("parameters");
        String facebookId = null;
        JSONObject object = null;
        if (parameters != null) {
            try {
                object = (JSONObject) new JSONTokener(parameters).nextValue();
            } catch (Exception e) {
                Log.d(TAG, "cannot parse the object");
            }
        }
        if (object != null && object.has("facebookId")) {
            facebookId = object.getString("facebookId");
        }

        CharSequence tickerText = intent.getStringExtra("tickerText");
        CharSequence contentTitle = intent.getStringExtra("contentTitle");
        CharSequence contentText = intent.getStringExtra("contentText");

        Intent notificationIntent = new Intent(context, Class.forName(context.getPackageName() + ".AppEntry"));

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        Notification notification = new Notification(icon, tickerText, when);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        RemoteViews contentView = new RemoteViews(context.getPackageName(), customLayout);

        contentView.setTextViewText(customLayoutTitle, contentTitle);
        contentView.setTextViewText(customLayoutDescription, contentText);

        contentView.setTextColor(customLayoutTitle, notification_text_color);
        contentView.setFloat(customLayoutTitle, "setTextSize",
                notification_title_size_factor * notification_text_size);
        contentView.setTextColor(customLayoutDescription, notification_text_color);
        contentView.setFloat(customLayoutDescription, "setTextSize",
                notification_description_size_factor * notification_text_size);

        if (facebookId != null) {
            Log.d(TAG, "bitmap not null");
            CreateNotificationTask cNT = new CreateNotificationTask();
            cNT.setParams(customLayoutImageContainer, NotifId, nm, notification, contentView);
            String src = "http://graph.facebook.com/" + facebookId + "/picture?type=normal";
            URL url = new URL(src);
            cNT.execute(url);
        } else {
            Log.d(TAG, "bitmap null");
            contentView.setImageViewResource(customLayoutImageContainer, customLayoutImage);
            notification.contentView = contentView;
            nm.notify(NotifId, notification);
        }
        NotifId++;

        if (ctxt != null) {
            parameters = parameters == null ? "" : parameters;
            ctxt.dispatchStatusEventAsync("COMING_FROM_NOTIFICATION", parameters);
        }

    } catch (Exception e) {
        Log.e(TAG, "Error activating application:", e);
    }
}