Example usage for org.json JSONArray getString

List of usage examples for org.json JSONArray getString

Introduction

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

Prototype

public String getString(int index) throws JSONException 

Source Link

Document

Get the string associated with an index.

Usage

From source file:org.apache.cordova.dialogs.Notification.java

/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArray of arguments for the plugin.
 * @param callbackContext   The callback context used when calling back into JavaScript.
 * @return                  True when the action was valid, false otherwise.
 *//*from   w  w  w . j  a v a  2s. c o m*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    /*
     * Don't run any of these if the current activity is finishing
     * in order to avoid android.view.WindowManager$BadTokenException
     * crashing the app. Just return true here since false should only
     * be returned in the event of an invalid action.
     */
    if (this.cordova.getActivity().isFinishing())
        return true;

    if (action.equals("beep")) {
        this.beep(args.getLong(0));
    } else if (action.equals("alert")) {
        this.alert(args.getString(0), args.getString(1), args.getString(2), callbackContext);
        return true;
    } else if (action.equals("confirm")) {
        this.confirm(args.getString(0), args.getString(1), args.getJSONArray(2), callbackContext);
        return true;
    } else if (action.equals("prompt")) {
        this.prompt(args.getString(0), args.getString(1), args.getJSONArray(2), args.getString(3),
                callbackContext);
        return true;
    } else if (action.equals("activityStart")) {
        this.activityStart(args.getString(0), args.getString(1));
    } else if (action.equals("activityStop")) {
        this.activityStop();
    } else if (action.equals("progressStart")) {
        this.progressStart(args.getString(0), args.getString(1));
    } else if (action.equals("progressValue")) {
        this.progressValue(args.getInt(0));
    } else if (action.equals("progressStop")) {
        this.progressStop();
    } else {
        return false;
    }

    // Only alert and confirm are async.
    callbackContext.success();
    return true;
}

From source file:org.apache.cordova.dialogs.Notification.java

/**
 * Builds and shows a native Android confirm dialog with given title, message, buttons.
 * This dialog only shows up to 3 buttons.  Any labels after that will be ignored.
 * The index of the button pressed will be returned to the JavaScript callback identified by callbackId.
 *
 * @param message           The message the dialog should display
 * @param title             The title of the dialog
 * @param buttonLabels      A comma separated list of button labels (Up to 3 buttons)
 * @param callbackContext   The callback context.
 *//*from  w w  w.  ja v  a  2  s . c  o m*/
public synchronized void confirm(final String message, final String title, final JSONArray buttonLabels,
        final CallbackContext callbackContext) {
    final CordovaInterface cordova = this.cordova;

    Runnable runnable = new Runnable() {
        public void run() {
            AlertDialog.Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
            dlg.setMessage(message);
            dlg.setTitle(title);
            dlg.setCancelable(true);

            // First button
            if (buttonLabels.length() > 0) {
                try {
                    dlg.setNegativeButton(buttonLabels.getString(0), new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 1));
                        }
                    });
                } catch (JSONException e) {
                }
            }

            // Second button
            if (buttonLabels.length() > 1) {
                try {
                    dlg.setNeutralButton(buttonLabels.getString(1), new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 2));
                        }
                    });
                } catch (JSONException e) {
                }
            }

            // Third button
            if (buttonLabels.length() > 2) {
                try {
                    dlg.setPositiveButton(buttonLabels.getString(2), new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 3));
                        }
                    });
                } catch (JSONException e) {
                }
            }
            dlg.setOnCancelListener(new AlertDialog.OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                    dialog.dismiss();
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
                }
            });

            changeTextDirection(dlg);
        };
    };
    this.cordova.getActivity().runOnUiThread(runnable);
}

From source file:org.apache.cordova.dialogs.Notification.java

/**
 * Builds and shows a native Android prompt dialog with given title, message, buttons.
 * This dialog only shows up to 3 buttons.  Any labels after that will be ignored.
 * The following results are returned to the JavaScript callback identified by callbackId:
 *     buttonIndex         Index number of the button selected
 *     input1            The text entered in the prompt dialog box
 *
 * @param message           The message the dialog should display
 * @param title             The title of the dialog
 * @param buttonLabels      A comma separated list of button labels (Up to 3 buttons)
 * @param callbackContext   The callback context.
 *//*from  w  w  w  . j a  v  a  2  s .c om*/
public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels,
        final String defaultText, final CallbackContext callbackContext) {

    final CordovaInterface cordova = this.cordova;

    Runnable runnable = new Runnable() {
        public void run() {
            final EditText promptInput = new EditText(cordova.getActivity());
            promptInput.setHint(defaultText);
            AlertDialog.Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
            dlg.setMessage(message);
            dlg.setTitle(title);
            dlg.setCancelable(true);

            dlg.setView(promptInput);

            final JSONObject result = new JSONObject();

            // First button
            if (buttonLabels.length() > 0) {
                try {
                    dlg.setNegativeButton(buttonLabels.getString(0), new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            try {
                                result.put("buttonIndex", 1);
                                result.put("input1",
                                        promptInput.getText().toString().trim().length() == 0 ? defaultText
                                                : promptInput.getText());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                        }
                    });
                } catch (JSONException e) {
                }
            }

            // Second button
            if (buttonLabels.length() > 1) {
                try {
                    dlg.setNeutralButton(buttonLabels.getString(1), new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            try {
                                result.put("buttonIndex", 2);
                                result.put("input1",
                                        promptInput.getText().toString().trim().length() == 0 ? defaultText
                                                : promptInput.getText());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                        }
                    });
                } catch (JSONException e) {
                }
            }

            // Third button
            if (buttonLabels.length() > 2) {
                try {
                    dlg.setPositiveButton(buttonLabels.getString(2), new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            try {
                                result.put("buttonIndex", 3);
                                result.put("input1",
                                        promptInput.getText().toString().trim().length() == 0 ? defaultText
                                                : promptInput.getText());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                        }
                    });
                } catch (JSONException e) {
                }
            }
            dlg.setOnCancelListener(new AlertDialog.OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                    dialog.dismiss();
                    try {
                        result.put("buttonIndex", 0);
                        result.put("input1", promptInput.getText().toString().trim().length() == 0 ? defaultText
                                : promptInput.getText());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                }
            });

            changeTextDirection(dlg);
        };
    };
    this.cordova.getActivity().runOnUiThread(runnable);
}

From source file:org.owasp.goatdroid.herdfinancial.rest.balances.BalancesResponse.java

static public HashMap<String, String> parseStatusAndBalances(String response) {

    JSONObject json;/*from   w  w w  .  ja v  a  2 s . c o m*/
    HashMap<String, String> results = new HashMap<String, String>();
    String errors = "";
    try {
        json = new JSONObject(response);
        if (json.getString("success").equals("false")) {
            results.put("success", "false");
            try {
                JSONArray errorArray = json.getJSONArray("errors");

                for (int count = 0; count < errorArray.length(); count++)
                    errors += "-" + errorArray.getString(count).toString() + "\n\n";

            } catch (JSONException e) {
                errors += "-" + json.getString("errors");
            }

            results.put("errors", errors);
            return results;

        } else {
            results.put("success", "true");
            results.put("checkingBalance", json.getString("checkingBalance"));
            results.put("savingsBalance", json.getString("savingsBalance"));
            return results;
        }

    } catch (JSONException e) {
        results.put("success", "false");
        results.put("errors", e.getMessage());
        return results;
    } catch (Exception e) {
        results.put("errors", e.getMessage());
        return results;
    }
}

From source file:com.amazon.dynamicparser.testResources.DummyContent.java

/**
 * Get content tags.//from  w  ww  .  j  a va 2  s.co m
 *
 * @return Content tags as String list.
 */
public List<String> getTags() {

    List<String> list = new ArrayList<>();
    try {
        JSONArray jsonArray = new JSONArray(mTags);
        for (int i = 0; i < jsonArray.length(); i++) {
            list.add(jsonArray.getString(i));
        }
    } catch (JSONException e) {
        Log.e(TAG, "There was an error", e);
        return list;
    }
    return list;
}

From source file:org.apache.cordova.contacts.ContactManager.java

/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArray of arguments for the plugin.
 * @param callbackContext   The callback context used when calling back into JavaScript.
 * @return                  True if the action was valid, false otherwise.
 *//*from  www  .j a v a 2  s.  c o  m*/
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
        throws JSONException {

    this.callbackContext = callbackContext;
    this.executeArgs = args;

    /**
     * Check to see if we are on an Android 1.X device.  If we are return an error as we
     * do not support this as of Cordova 1.0.
     */
    if (android.os.Build.VERSION.RELEASE.startsWith("1.")) {
        callbackContext.sendPluginResult(
                new PluginResult(PluginResult.Status.ERROR, ContactManager.NOT_SUPPORTED_ERROR));
        return true;
    }

    /**
     * Only create the contactAccessor after we check the Android version or the program will crash
     * older phones.
     */
    if (this.contactAccessor == null) {
        this.contactAccessor = new ContactAccessorSdk5(this.cordova);
    }

    if (action.equals("search")) {
        final JSONArray filter = args.getJSONArray(0);
        final JSONObject options = args.get(1) == null ? null : args.getJSONObject(1);
        this.cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                JSONArray res = contactAccessor.search(filter, options);
                callbackContext.success(res);
            }
        });
    } else if (action.equals("save")) {
        final JSONObject contact = args.getJSONObject(0);
        this.cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                JSONObject res = null;
                String id = contactAccessor.save(contact);
                if (id != null) {
                    try {
                        res = contactAccessor.getContactById(id);
                    } catch (JSONException e) {
                        Log.e(LOG_TAG, "JSON fail.", e);
                    }
                }
                if (res != null) {
                    callbackContext.success(res);
                } else {
                    callbackContext
                            .sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR));
                }
            }
        });
    } else if (action.equals("remove")) {
        final String contactId = args.getString(0);
        this.cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                if (contactAccessor.remove(contactId)) {
                    callbackContext.success();
                } else {
                    callbackContext
                            .sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR));
                }
            }
        });
    } else if (action.equals("pickContact")) {
        pickContactAsync();
    } else {
        return false;
    }
    return true;
}

From source file:org.owasp.goatdroid.fourgoats.rest.searchforfriends.SearchForFriendsResponse.java

static public ArrayList<HashMap<String, String>> parseSearchForFriendsResponse(String response) {

    JSONObject json;//from  w  w  w .jav a  2 s . com
    ArrayList<HashMap<String, String>> results = new ArrayList<HashMap<String, String>>();
    String errors = "";

    try {
        json = new JSONObject(response);
        if (json.getString("success").equals("true")) {
            JSONArray friendArray = json.getJSONArray("users");
            for (int count = 0; count < friendArray.length(); count++) {
                HashMap<String, String> resultsMap = new HashMap<String, String>();
                resultsMap.put("success", "true");
                HashMap<String, String> friend = new HashMap<String, String>();

                if (friendArray.getJSONObject(count).has("userID"))
                    friend.put("userID", (String) friendArray.getJSONObject(count).get("userID"));
                if (friendArray.getJSONObject(count).has("userName"))
                    friend.put("userName", (String) friendArray.getJSONObject(count).get("userName"));
                if (friendArray.getJSONObject(count).has("firstName"))
                    friend.put("firstName", (String) friendArray.getJSONObject(count).get("firstName"));
                if (friendArray.getJSONObject(count).has("lastName"))
                    friend.put("lastName", (String) friendArray.getJSONObject(count).get("lastName"));
                results.add(resultsMap);
                if (friend.size() > 0)
                    results.add(friend);
            }
        } else {
            HashMap<String, String> resultsMap = new HashMap<String, String>();
            resultsMap.put("success", "false");
            try {
                JSONArray errorArray = json.getJSONArray("errors");

                for (int count = 0; count < errorArray.length(); count++)
                    errors += errorArray.getString(count).toString() + "\n\n";

            } catch (JSONException e) {
                errors += json.getString("errors");
            }
            resultsMap.put("errors", errors);
            results.add(resultsMap);
        }
    } catch (JSONException e) {
        try {
            json = new JSONObject(response);
            HashMap<String, String> resultsMap = new HashMap<String, String>();
            resultsMap.put("success", "true");
            results.add(resultsMap);
            HashMap<String, String> friend = new HashMap<String, String>();
            if (json.getJSONObject("users").has("userID"))
                friend.put("userID", (String) json.getJSONObject("users").get("userID"));
            if (json.getJSONObject("users").has("userName"))
                friend.put("userName", (String) json.getJSONObject("users").get("userName"));
            if (json.getJSONObject("users").has("firstName"))
                friend.put("firstName", (String) json.getJSONObject("users").get("firstName"));
            if (json.getJSONObject("users").has("lastName"))
                friend.put("lastName", (String) json.getJSONObject("users").get("lastName"));
            if (friend.size() > 0)
                results.add(friend);

        } catch (JSONException e1) {
            /*
             * We don't care if it falls through here.
             */
            e1.getMessage();
        }
    }
    return results;
}

From source file:org.dasein.cloud.joyent.JoyentDataCenter.java

@Override
public @Nonnull Collection<Region> listRegions() throws InternalException, CloudException {
    JoyentMethod method = new JoyentMethod(provider);
    String json = method.doGetJson(provider.getEndpoint(), "datacenters");

    try {/*w  ww.  j  a v a2 s .c  o m*/
        ArrayList<Region> regions = new ArrayList<Region>();

        JSONObject ob = new JSONObject(json);
        JSONArray ids = ob.names();

        for (int i = 0; i < ids.length(); i++) {
            String regionId = ids.getString(i);
            Region r = new Region();

            r.setActive(true);
            r.setAvailable(true);
            r.setJurisdiction("US");
            r.setName(regionId);
            r.setProviderRegionId(regionId);
            regions.add(r);
        }
        return regions;
    } catch (JSONException e) {
        throw new CloudException(e);
    }
}

From source file:com.phonegap.GeoBroker.java

/**
 * Executes the request and returns PluginResult.
 * //from   w w  w  .java  2s. c om
 * @param action       The action to execute.
 * @param args          JSONArry of arguments for the plugin.
 * @param callbackId   The callback id used when calling back into JavaScript.
 * @return             A PluginResult object with a status and message.
 */
public PluginResult execute(String action, JSONArray args, String callbackId) {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";

    try {
        if (action.equals("getCurrentLocation")) {
            this.getCurrentLocation(args.getBoolean(0), args.getInt(1), args.getInt(2));
        } else if (action.equals("start")) {
            String s = this.start(args.getString(0), args.getBoolean(1), args.getInt(2), args.getInt(3));
            return new PluginResult(status, s);
        } else if (action.equals("stop")) {
            this.stop(args.getString(0));
        }
        return new PluginResult(status, result);
    } catch (JSONException e) {
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }
}

From source file:com.opera.toast.ToastNotification.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    String message = args.getString(0);

    // used to log the text and can be seen in LogCat
    Log.d("Toast Plugin", "Calling the Toast...");
    Log.d("Toast Plugin", message);

    if (action.equals("showToast")) {
        this.showToast(message, callbackContext);
        return true;
    }//from   w  w  w  .ja  v a  2s.  c  o m
    return false;
}