Example usage for org.json JSONArray optString

List of usage examples for org.json JSONArray optString

Introduction

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

Prototype

public String optString(int index) 

Source Link

Document

Get the optional string value associated with an index.

Usage

From source file:com.ca.mas.cordova.storage.MASStoragePlugin.java

private void findByUsingKeyAndModeCloud(final JSONArray args, final CallbackContext callbackContext) {
    try {/*  w w  w.j a  v a2s.c  o m*/
        MASStorage storage = new MASSecureStorage();
        String key = args.optString(0);
        int segment_0 = args.getInt(1);
        int segment = fetchSegmentCloud(segment_0);

        storage.findByKey(key, segment, new MASCallback() {
            @Override
            public void onSuccess(Object result) {
                JSONObject response = null;
                try {
                    response = getResultJson(result);
                    success(callbackContext, response, false);
                } catch (Exception ex) {
                    LOG.e(TAG, ex.getMessage());
                    callbackContext.error(getError(ex));
                }
            }

            @Override
            public void onError(Throwable e) {
                LOG.e(TAG, e.getMessage());
                callbackContext.error(getError(e));
            }
        });
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
        callbackContext.error(getError(e));
    }
}

From source file:com.ca.mas.cordova.storage.MASStoragePlugin.java

private void deleteByUsingKeyAndModeCloud(final JSONArray args, final CallbackContext callbackContext) {
    try {// w w w  .  jav a 2s.  c  o  m
        MASStorage storage = new MASSecureStorage();
        String key = args.optString(0);
        int segment_0 = args.getInt(1);
        int segment = fetchSegmentCloud(segment_0);

        storage.delete(key, segment, new MASCallback<Void>() {
            @Override
            public void onSuccess(Void result) {
                success(callbackContext, true, false);
            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, e.getMessage(), e);
                callbackContext.error(getError(e));
            }
        });
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
        callbackContext.error(getError(e));
    }
}

From source file:com.yanzhenjie.nohttp.HttpHeaders.java

@Override
public void setJSONString(String jsonString) throws JSONException {
    clear();//from  w ww.j a  v a 2  s. c  o m
    JSONObject jsonObject = new JSONObject(jsonString);
    Iterator<String> keySet = jsonObject.keys();
    while (keySet.hasNext()) {
        String key = keySet.next();
        String value = jsonObject.optString(key);
        JSONArray values = new JSONArray(value);
        for (int i = 0; i < values.length(); i++)
            add(key, values.optString(i));
    }
}

From source file:com.quantcast.measurement.service.QCLocation.java

private boolean containsString(JSONArray arr, String s) {
    String tmp;/*from ww  w. ja  v  a2 s . co m*/
    for (int i = 0; i < arr.length(); i++) {
        tmp = arr.optString(i);
        if (tmp != null && tmp.equals(s))
            return true;
    }
    return false;
}

From source file:com.phonegap.FileTransfer.java

/**
 * Convenience method to read a parameter from the list of JSON args.
 * @param args         the args passed to the Plugin
 * @param position      the position to retrieve the arg from
 * @param defaultString the default to be used if the arg does not exist
 * @return String with the retrieved value
 *//*w w  w. ja va 2s .  c  o m*/
private String getArgument(JSONArray args, int position, String defaultString) {
    String arg = defaultString;
    if (args.length() >= position) {
        arg = args.optString(position);
        if (arg == null || "null".equals(arg)) {
            arg = defaultString;
        }
    }
    return arg;
}

From source file:de.limexcomputer.cordova.plugin.rotationlock.RotationLock.java

/**
 * Executes the request.// w w  w  .ja v a 2 s .c  o m
 *
 * This method is called from the WebView thread.
 * To do a non-trivial amount of work, use:
 *     cordova.getThreadPool().execute(runnable);
 *
 * To run on the UI thread, use:
 *     cordova.getActivity().runOnUiThread(runnable);
 *
 * @param action   The action to execute.
 * @param args     The exec() arguments in JSON form.
 * @param callback The callback context used when calling back into JavaScript.
 * @return         Whether the action was valid.
 */
@Override
public boolean execute(String action, JSONArray args, CallbackContext callback) throws JSONException {

    if (!action.equalsIgnoreCase("setOrientation")) {
        return false;
    }

    String orientation = args.optString(0);

    Activity activity = this.cordova.getActivity();

    // refer to https://github.com/their/pg-plugin-screen-orientation/blob/master/src/ScreenOrientation.java

    if (orientation.equals(UNSPECIFIED)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    } else if (orientation.equals(LANDSCAPE)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } else if (orientation.equals(PORTRAIT)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else if (orientation.equals(USER)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
    } else if (orientation.equals(BEHIND)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND);
    } else if (orientation.equals(SENSOR)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    } else if (orientation.equals(NOSENSOR)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    } else if (orientation.equals(SENSOR_LANDSCAPE)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    } else if (orientation.equals(SENSOR_PORTRAIT)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    } else if (orientation.equals(REVERSE_LANDSCAPE)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
    } else if (orientation.equals(REVERSE_PORTRAIT)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
    } else if (orientation.equals(FULL_SENSOR)) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
    }

    callback.success(orientation);
    return true;
}

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));
    }//w  w  w .j a v a2s .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:net.yoik.cordova.plugins.screenorientation.YoikScreenOrientation.java

private boolean routeScreenOrientation(JSONArray args, CallbackContext callbackContext) {

    String action = args.optString(0);

    if (action.equals("set")) {

        String orientation = args.optString(1);

        Log.d(TAG, "Requested ScreenOrientation: " + orientation);

        Activity activity = cordova.getActivity();

        if (orientation.equals(UNLOCKED)) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        } else if (orientation.equals(LANDSCAPE_PRIMARY)) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else if (orientation.equals(PORTRAIT_PRIMARY)) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else if (orientation.equals(LANDSCAPE)) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else if (orientation.equals(PORTRAIT)) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        } else if (orientation.equals(LANDSCAPE_SECONDARY)) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        } else if (orientation.equals(PORTRAIT_SECONDARY)) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        }/*  ww  w.  ja  va2  s  . c  om*/

        callbackContext.success();
        return true;

    } else {
        callbackContext.error("ScreenOrientation not recognised");
        return false;
    }
}

From source file:com.google.android.apps.muzei.api.internal.SourceState.java

public void readJson(JSONObject jsonObject) throws JSONException {
    JSONObject artworkJsonObject = jsonObject.optJSONObject("currentArtwork");
    if (artworkJsonObject != null) {
        mCurrentArtwork = Artwork.fromJson(artworkJsonObject);
    }//from ww  w  . j  a  v a 2 s.  c o  m
    mDescription = jsonObject.optString("description");
    mWantsNetworkAvailable = jsonObject.optBoolean("wantsNetworkAvailable");
    mUserCommands.clear();
    JSONArray commandsSerialized = jsonObject.optJSONArray("userCommands");
    if (commandsSerialized != null && commandsSerialized.length() > 0) {
        int length = commandsSerialized.length();
        for (int i = 0; i < length; i++) {
            mUserCommands.add(UserCommand.deserialize(commandsSerialized.optString(i)));
        }
    }
}

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

public static Field fromJson(SymbolTable symbolTable, JSONObject fieldJson) {
    String type = fieldJson.optString("type");
    if (type == null || !type.equals("multi_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   w w w. j av  a2  s.c om
    int nominalWidth = fieldJson.has("nominal_width") ? fieldJson.optInt("nominal_width") : 0;
    String compute = fieldJson.has("compute") ? fieldJson.optString("compute") : null;
    return new MultiChoiceField(symbolTable, name, label, description, defaultValue, choices, nominalWidth,
            compute);
}