Example usage for org.json JSONArray isNull

List of usage examples for org.json JSONArray isNull

Introduction

In this page you can find the example usage for org.json JSONArray isNull.

Prototype

public boolean isNull(int index) 

Source Link

Document

Determine if the value is null.

Usage

From source file:com.example.socketio.lib.IOConnection.java

/**
 * Transport message. {@link IOTransport} calls this, when a message has
 * been received./* w w  w  .  j a  v  a  2 s. c  om*/
 * 
 * @param text
 *            the text
 */
public void transportMessage(String text) {
    logger.info("< " + text);
    IOMessage message;
    try {
        message = new IOMessage(text);
    } catch (Exception e) {
        error(new SocketIOException("Garbage from server: " + text, e));
        return;
    }
    resetTimeout();
    switch (message.getType()) {
    case IOMessage.TYPE_DISCONNECT:
        try {
            findCallback(message).onDisconnect();
        } catch (Exception e) {
            error(new SocketIOException("Exception was thrown in onDisconnect()", e));
        }
        break;
    case IOMessage.TYPE_CONNECT:
        try {
            if (firstSocket != null && "".equals(message.getEndpoint())) {
                if (firstSocket.getNamespace().equals("")) {
                    firstSocket.getCallback().onConnect();
                } else {
                    IOMessage connect = new IOMessage(IOMessage.TYPE_CONNECT, firstSocket.getNamespace(), "");
                    sendPlain(connect.toString());
                }
            } else {
                findCallback(message).onConnect();
            }
            firstSocket = null;
        } catch (Exception e) {
            error(new SocketIOException("Exception was thrown in onConnect()", e));
        }
        break;
    case IOMessage.TYPE_HEARTBEAT:
        sendPlain("2::");
        break;
    case IOMessage.TYPE_MESSAGE:
        try {
            findCallback(message).onMessage(message.getData(), remoteAcknowledge(message));
        } catch (Exception e) {
            error(new SocketIOException(
                    "Exception was thrown in onMessage(String).\n" + "Message was: " + message.toString(), e));
        }
        break;
    case IOMessage.TYPE_JSON_MESSAGE:
        try {
            JSONObject obj = null;
            String data = message.getData();
            if (data.trim().equals("null") == false)
                obj = new JSONObject(data);
            try {
                findCallback(message).onMessage(obj, remoteAcknowledge(message));
            } catch (Exception e) {
                error(new SocketIOException("Exception was thrown in onMessage(JSONObject).\n" + "Message was: "
                        + message.toString(), e));
            }
        } catch (JSONException e) {
            logger.warning("Malformated JSON received");
        }
        break;
    case IOMessage.TYPE_EVENT:
        try {
            JSONObject event = new JSONObject(message.getData());
            Object[] argsArray;
            if (event.has("args")) {
                JSONArray args = event.getJSONArray("args");
                argsArray = new Object[args.length()];
                for (int i = 0; i < args.length(); i++) {
                    if (args.isNull(i) == false)
                        argsArray[i] = args.get(i);
                }
            } else
                argsArray = new Object[0];
            String eventName = event.getString("name");
            try {
                findCallback(message).on(eventName, remoteAcknowledge(message), argsArray);
            } catch (Exception e) {
                error(new SocketIOException("Exception was thrown in on(String, JSONObject[]).\n"
                        + "Message was: " + message.toString(), e));
            }
        } catch (JSONException e) {
            logger.warning("Malformated JSON received");
        }
        break;
    case IOMessage.TYPE_ACK:
        String[] data = message.getData().split("\\+", 2);
        if (data.length == 2) {
            try {
                int id = Integer.parseInt(data[0]);
                IOAcknowledge ack = acknowledge.get(id);
                if (ack == null)
                    logger.warning("Received unknown ack packet");
                else {
                    JSONArray array = new JSONArray(data[1]);
                    Object[] args = new Object[array.length()];
                    for (int i = 0; i < args.length; i++) {
                        args[i] = array.get(i);
                    }
                    ack.ack(args);
                }
            } catch (NumberFormatException e) {
                logger.warning(
                        "Received malformated Acknowledge! This is potentially filling up the acknowledges!");
            } catch (JSONException e) {
                logger.warning("Received malformated Acknowledge data!");
            }
        } else if (data.length == 1) {
            sendPlain("6:::" + data[0]);
        }
        break;
    case IOMessage.TYPE_ERROR: {
        try {
            findCallback(message).onError(new SocketIOException(message.getData()));
        } catch (SocketIOException e) {
            error(e);
        }
        if (message.getData().endsWith("+0")) {
            SendErrorMessageWithBroadcast(SocketIOAction.SOCKETIO_ERRORMESSAGE_SERVERREJECTCONNECTION);
            // We are advised to disconnect
            cleanup();
        }
    }
        break;
    case IOMessage.TYPE_NOOP:
        break;
    default:
        logger.warning("Unkown type received" + message.getType());
        break;
    }
}

From source file:com.amazon.cordova.plugin.PushPlugin.java

/**
 * @see Plugin#execute(String, JSONArray, String)
 */// w w w.ja  v  a2 s.  c o m
@Override
public boolean execute(final String request, final JSONArray args, CallbackContext callbackContext)
        throws JSONException {
    try {
        // check ADM readiness
        ADMReadiness ready = isPushPluginReady();
        if (ready == ADMReadiness.NON_AMAZON_DEVICE) {
            callbackContext.error(NON_AMAZON_DEVICE_ERROR);
            return false;
        } else if (ready == ADMReadiness.ADM_NOT_SUPPORTED) {
            callbackContext.error(ADM_NOT_SUPPORTED_ERROR);
            return false;
        } else if (callbackContext == null) {
            LOG.e(TAG, "CallbackConext is null. Notification to WebView is not possible. Can not proceed.");
            return false;
        }

        // Process the request here
        if (REGISTER.equals(request)) {

            if (args == null) {
                LOG.e(TAG, REGISTER_OPTIONS_NULL);
                callbackContext.error(REGISTER_OPTIONS_NULL);
                return false;
            }

            // parse args to get eventcallback name
            if (args.isNull(0)) {
                LOG.e(TAG, ECB_NOT_SPECIFIED);
                callbackContext.error(ECB_NOT_SPECIFIED);
                return false;
            }

            JSONObject jo = args.getJSONObject(0);
            if (jo.getString("ecb").isEmpty()) {
                LOG.e(TAG, ECB_NAME_NOT_SPECIFIED);
                callbackContext.error(ECB_NAME_NOT_SPECIFIED);
                return false;
            }
            callbackContext.success(REGISTRATION_SUCCESS_RESPONSE);
            notificationHandlerCallBack = jo.getString(ECB);
            String regId = adm.getRegistrationId();
            LOG.d(TAG, "regId = " + regId);
            if (regId == null) {
                adm.startRegister();
            } else {
                sendRegistrationIdWithEvent(REGISTER_EVENT, regId);
            }

            // see if there are any messages while app was in background and
            // launched via app icon
            LOG.d(TAG, "checking for offline message..");
            deliverPendingMessageAndCancelNotifiation();
            return true;

        } else if (UNREGISTER.equals(request)) {
            adm.startUnregister();
            callbackContext.success(UNREGISTRATION_SUCCESS_RESPONSE);
            return true;
        } else {
            LOG.e(TAG, "Invalid action : " + request);
            callbackContext.error("Invalid action : " + request);
            return false;
        }
    } catch (final Exception e) {
        callbackContext.error(e.getMessage());
    }

    return false;
}

From source file:com.samsung.spen.SpenPlugin.java

/**
 * //from  w w  w  . jav a  2 s .co  m
 * @param args
 *                 JSON array of options sent from the script.
 * @param surfaceType
 *                int
 * @param callbackContext
 *                CallbackContext
 * @return   options
 *                SpenTrayBarOptions
 * @throws JSONException
 */
private SpenTrayBarOptions createTrayBarOptions(JSONArray args, int surfaceType,
        CallbackContext callbackContext) throws JSONException {
    if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
        Log.d(TAG, "Inside createTrayBarOptions");
    }

    String tempId = args.getString(ID);
    if (tempId != null) {
        tempId = tempId.trim();
        if (tempId.length() > MAX_ID_LENGTH) {
            tempId = tempId.substring(0, MAX_ID_LENGTH);
        }
    }

    final String id = tempId;
    if (id == null || id.equals("") || id.equals("null") || !id.matches("^[ !#-)+-.0-9;=@-Z^-{}~]+$")) {
        SpenException.sendPluginResult(SpenExceptionType.INVALID_SURFACE_ID, callbackContext);
        return null;
    }

    int sPenFlags = args.optInt(SPEN_FLAGS, Integer.MIN_VALUE);
    if (sPenFlags == Integer.MIN_VALUE || sPenFlags > Utils.MAX_FLAGS_VALUE
            || sPenFlags < Utils.MIN_FLAGS_VALUE) {
        SpenException.sendPluginResult(SpenExceptionType.INVALID_FLAGS, callbackContext);
        return null;
    }

    int returnType = args.optInt(RETURN_TYPE);
    if (returnType != Utils.RETURN_TYPE_IMAGE_DATA && returnType != Utils.RETURN_TYPE_IMAGE_URI
            && returnType != Utils.RETURN_TYPE_TEXT) {

        SpenException.sendPluginResult(SpenExceptionType.INVALID_RETURN_TYPE, callbackContext);
        return null;
    }

    String backgroundColor = args.getString(BACKGROUND_COLOR);

    String imagePath = args.getString(IMAGE_PATH);
    if (imagePath.equals("") || imagePath.equals("null")) {
        imagePath = null;
    } else {
        imagePath = Uri.decode(imagePath);
        String truncatedPath = truncateQueryPart(imagePath);
        File file = new File(truncatedPath);
        if (file.exists()) {
            imagePath = truncatedPath;
        }
    }

    int bgImageScaleType = args.optInt(BACKGROUND_IMAGE_SCALE_TYPE);
    if (bgImageScaleType != Utils.BACKGROUND_IMAGE_MODE_CENTER
            && bgImageScaleType != Utils.BACKGROUND_IMAGE_MODE_FIT
            && bgImageScaleType != Utils.BACKGROUND_IMAGE_MODE_STRETCH
            && bgImageScaleType != Utils.BACKGROUND_IMAGE_MODE_TILE) {
        bgImageScaleType = Utils.BACKGROUND_IMAGE_MODE_FIT;
    }

    int imageUriScaleType = args.optInt(IMAGE_URI_SCALE_TYPE);
    if (imageUriScaleType != Utils.IMAGE_URI_MODE_CENTER && imageUriScaleType != Utils.IMAGE_URI_MODE_FIT
            && imageUriScaleType != Utils.IMAGE_URI_MODE_TILE
            && imageUriScaleType != Utils.IMAGE_URI_MODE_STRETCH) {
        imageUriScaleType = Utils.IMAGE_URI_MODE_FIT;
    }

    if (surfaceType == Utils.SURFACE_INLINE) {
        if ((sPenFlags & Utils.FLAG_ADD_PAGE) == Utils.FLAG_ADD_PAGE) {
            if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                Log.d(TAG, "Add Page is not supported in Inline");
            }
            sPenFlags = sPenFlags & ~Utils.FLAG_ADD_PAGE;
        }
    } else if (surfaceType == Utils.SURFACE_POPUP) {

        if ((sPenFlags & Utils.FLAG_EDIT) == Utils.FLAG_EDIT) {
            if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                Log.d(TAG, "Edit Page is not supported in Popup");
            }
            sPenFlags = sPenFlags & ~Utils.FLAG_EDIT;
        }

        if ((sPenFlags & Utils.FLAG_PEN) == Utils.FLAG_PEN) {
            if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                Log.d(TAG, "Pen option is provided by default, is not configurable in Popup");
            }
            sPenFlags = sPenFlags & ~Utils.FLAG_PEN;

        }

        if ((sPenFlags & Utils.FLAG_ERASER) == Utils.FLAG_ERASER) {
            if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                Log.d(TAG, "Eraser option is provided by default, is not configurable in Popup");
            }
            sPenFlags = sPenFlags & ~Utils.FLAG_ERASER;

        }

        if ((sPenFlags & Utils.FLAG_UNDO_REDO) == Utils.FLAG_UNDO_REDO) {
            if (Log.isLoggable(Utils.SPEN, Log.DEBUG)) {
                Log.d(TAG, "Undo Redo option is provided by default, is not configurable in Popup");
            }
            sPenFlags = sPenFlags & ~Utils.FLAG_UNDO_REDO;

        }
    } else {
        SpenException.sendPluginResult(SpenExceptionType.INVALID_SURFACE_TYPE, callbackContext);
        return null;
    }

    if ((sPenFlags & Utils.FLAG_TEXT_RECOGNITION) == Utils.FLAG_TEXT_RECOGNITION
            || (sPenFlags & Utils.FLAG_SHAPE_RECOGNITION) == Utils.FLAG_SHAPE_RECOGNITION) {
        sPenFlags = sPenFlags | Utils.FLAG_SELECTION;
    }

    SpenTrayBarOptions options = new SpenTrayBarOptions(sPenFlags);
    options.setId(id);
    options.setIsfeatureEnabled(mSpenState == SPEN_AND_HAND_SUPPORTED ? true : false);
    options.setColor(backgroundColor);
    options.setBgImageScaleType(bgImageScaleType);
    options.setImageUriScaleType(imageUriScaleType);
    options.setReturnType(returnType);
    options.setSurfaceType(surfaceType);
    options.setImagePath(imagePath);
    options.setDensity(mActivity.getApplicationContext().getResources().getDisplayMetrics().density);

    if (surfaceType == Utils.SURFACE_INLINE) {
        long xRect = 0, yRect = 0, width = 0, height = 0, xBodyRect = 0, yBodyRect = 0;
        if (args.isNull(RECTANGLE_X_VALUE) || args.isNull(RECTANGLE_Y_VALUE) || args.isNull(WIDTH)
                || args.isNull(HEIGHT)) {
            SpenException.sendPluginResult(SpenExceptionType.INVALID_INLINE_CORDINATES, callbackContext);
            return null;
        } else {
            xRect = args.optLong(RECTANGLE_X_VALUE, Integer.MIN_VALUE);
            yRect = args.optLong(RECTANGLE_Y_VALUE, Integer.MIN_VALUE);
            width = args.optLong(WIDTH, Integer.MIN_VALUE);
            height = args.optLong(HEIGHT, Integer.MIN_VALUE);
            xBodyRect = args.optLong(BODY_RECTANGLE_X_VALUE, Integer.MIN_VALUE);
            yBodyRect = args.optLong(BODY_RECTANGLE_Y_VALUE, Integer.MAX_VALUE);
            if (xRect == Integer.MIN_VALUE || yRect == Integer.MIN_VALUE || width == Integer.MIN_VALUE
                    || height == Integer.MIN_VALUE || xBodyRect == Integer.MIN_VALUE
                    || yBodyRect == Integer.MIN_VALUE || xRect > (long) Integer.MAX_VALUE
                    || yRect > (long) Integer.MAX_VALUE || width > (long) Integer.MAX_VALUE
                    || height > (long) Integer.MAX_VALUE || xBodyRect > (long) Integer.MAX_VALUE
                    || yBodyRect > (long) Integer.MAX_VALUE) {
                SpenException.sendPluginResult(SpenExceptionType.INVALID_INLINE_CORDINATES, callbackContext);
                return null;
            }
        }
        SurfacePosition surfacePosition = new SurfacePosition(mActivity.getApplicationContext(), (int) width,
                (int) height, (int) xRect - (int) xBodyRect, (int) yRect - (int) yBodyRect);
        if (!surfacePosition.isSurfaceValid(options, mActivity.getApplicationContext())) {
            SpenException.sendPluginResult(SpenExceptionType.INVALID_INLINE_CORDINATES, callbackContext);
            return null;
        }
        options.setSurfacePosition(surfacePosition);
    } else if (surfaceType == Utils.SURFACE_POPUP) {
        long popupWidth = 0, popupHeight = 0;
        popupWidth = args.optLong(POPUP_WIDTH, Integer.MIN_VALUE);
        popupHeight = args.optLong(POPUP_HEIGHT, Integer.MIN_VALUE);
        SurfacePosition surfacePosition = new SurfacePosition(mActivity.getApplicationContext(),
                (int) popupWidth, (int) popupHeight);
        options.setSurfacePosition(surfacePosition);
    }
    return options;
}

From source file:cz.muni.fi.japanesedictionary.entity.Translation.java

/**
 * Takes json string and parses it list of English meanings.
 * //from w w  w .  j a va 2s .  c  om
 * @param jsonString - JSON string to be parsed
 */
public void parseEnglish(String jsonString) {
    if (jsonString == null) {
        return;
    }
    JSONArray language_senses;
    try {
        language_senses = new JSONArray(jsonString);
    } catch (JSONException e) {
        Log.w(LOG_TAG, "getting parseEnglish()  initial expression failed: " + e.toString());
        return;
    }

    for (int j = 0; j < language_senses.length(); j++) {
        if (!language_senses.isNull(j)) {
            List<String> sense;
            try {
                sense = parseOneSense(language_senses.getJSONArray(j));
                this.addEnglishSense(sense);
            } catch (JSONException e) {
                Log.w(LOG_TAG, "getting parseEnglish() expression failed: " + e.toString());
            }
        }
    }
}

From source file:cz.muni.fi.japanesedictionary.entity.Translation.java

/**
 * Takes json string and parses it list of Dutch meanings.
 * //  w w w.j  a  v a  2 s  .com
 * @param jsonString - JSON string to be parsed
 */
public void parseDutch(String jsonString) {
    if (jsonString == null) {
        return;
    }
    JSONArray language_senses;
    try {
        language_senses = new JSONArray(jsonString);
    } catch (JSONException e) {
        Log.w(LOG_TAG, "getting parseDutch()  initial expression failed: " + e.toString());
        return;
    }

    for (int j = 0; j < language_senses.length(); j++) {
        if (!language_senses.isNull(j)) {
            List<String> sense;
            try {
                sense = parseOneSense(language_senses.getJSONArray(j));
                this.addDutchSense(sense);
            } catch (JSONException e) {
                Log.w(LOG_TAG, "getting parseDutch() expression failed: " + e.toString());
            }
        }
    }
}

From source file:cz.muni.fi.japanesedictionary.entity.Translation.java

/**
 * Takes json string and parses it list of French meanings.
 * //from   w ww  .j a va 2s. c o  m
 * @param jsonString - JSON string to be parsed
 */
public void parseFrench(String jsonString) {
    if (jsonString == null) {
        return;
    }
    JSONArray language_senses;
    try {
        language_senses = new JSONArray(jsonString);
    } catch (JSONException e) {
        Log.w(LOG_TAG, "getting parseFrench()  initial expression failed: " + e.toString());
        return;
    }

    for (int j = 0; j < language_senses.length(); j++) {
        if (!language_senses.isNull(j)) {
            List<String> sense;
            try {
                sense = parseOneSense(language_senses.getJSONArray(j));
                this.addFrenchSense(sense);
            } catch (JSONException e) {
                Log.w(LOG_TAG, "getting parseFrench() expression failed: " + e.toString());
            }
        }
    }
}

From source file:cz.muni.fi.japanesedictionary.entity.Translation.java

/**
 * Takes json string and parses it list of German meanings.
 * /*ww  w.java  2s . c  o m*/
 * @param jsonString - JSON string to be parsed
 */
public void parseGerman(String jsonString) {
    if (jsonString == null) {
        return;
    }
    JSONArray language_senses;
    try {
        language_senses = new JSONArray(jsonString);
    } catch (JSONException e) {
        Log.w(LOG_TAG, "getting parseGerman()  initial expression failed: " + e.toString());
        return;
    }

    for (int j = 0; j < language_senses.length(); j++) {
        if (!language_senses.isNull(j)) {
            List<String> sense;
            try {
                sense = parseOneSense(language_senses.getJSONArray(j));
                this.addGermanSense(sense);
            } catch (JSONException e) {
                Log.w(LOG_TAG, "getting parseGerman() expression failed: " + e.toString());
            }
        }
    }
}

From source file:cz.muni.fi.japanesedictionary.entity.Translation.java

/**
 * Takes json string and parses it list of Russian meanings.
 *
 * @param jsonString - JSON string to be parsed
 *///from  w  ww.  jav  a 2s .c  om
public void parseRussian(String jsonString) {
    if (jsonString == null) {
        return;
    }
    JSONArray language_senses;
    try {
        language_senses = new JSONArray(jsonString);
    } catch (JSONException e) {
        Log.w(LOG_TAG, "getting parseRussian()  initial expression failed: " + e.toString());
        return;
    }

    for (int j = 0; j < language_senses.length(); j++) {
        if (!language_senses.isNull(j)) {
            List<String> sense;
            try {
                sense = parseOneSense(language_senses.getJSONArray(j));
                this.addRussianSense(sense);
            } catch (JSONException e) {
                Log.w(LOG_TAG, "getting parseRussian() expression failed: " + e.toString());
            }
        }
    }
}