Example usage for org.json JSONArray getInt

List of usage examples for org.json JSONArray getInt

Introduction

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

Prototype

public int getInt(int index) throws JSONException 

Source Link

Document

Get the int value associated with an index.

Usage

From source file:com.funzio.pure2D.particles.nova.vo.NovaVO.java

protected static ArrayList<Integer> getListInt(final JSONObject json, final String field) throws JSONException {
    // field check
    if (!json.has(field)) {
        return null;
    }/*from   ww w. ja  v  a2 s  . c om*/

    final ArrayList<Integer> result = new ArrayList<Integer>();
    try {
        final JSONArray array = json.getJSONArray(field);
        if (array != null) {
            final int size = array.length();
            for (int i = 0; i < size; i++) {
                result.add(array.getInt(i));
            }
        }
    } catch (JSONException e) {
        // single value
        result.add(json.getInt(field));
    }

    return result;
}

From source file:com.funzio.pure2D.particles.nova.vo.NovaVO.java

protected static ArrayList<GLColor> getListColor(final JSONObject json, final String field)
        throws JSONException {
    // field check
    if (!json.has(field)) {
        return null;
    }//w  w w . j  ava 2  s. c  o m

    final ArrayList<GLColor> result = new ArrayList<GLColor>();
    try {
        final JSONArray array = json.getJSONArray(field);
        if (array != null) {
            final int size = array.length();
            for (int i = 0; i < size; i++) {
                if (array.get(i) instanceof String) {
                    result.add(new GLColor(Color.parseColor(array.getString(i))));
                } else {
                    result.add(new GLColor(array.getInt(i)));
                }
            }
        }
    } catch (JSONException e) {
        // single value
        if (json.get(field) instanceof String) {
            result.add(new GLColor(Color.parseColor(json.getString(field))));
        } else {
            result.add(new GLColor(json.getInt(field)));
        }
    }

    return result;
}

From source file:fr.bmartel.android.fadecandy.model.FadecandyConfig.java

public FadecandyConfig(JSONObject config) {

    try {/*from   w w w.j a v a  2 s. co m*/

        if (config.has(Constants.CONFIG_LISTEN) && config.has(Constants.CONFIG_VERBOSE)
                && config.has(Constants.CONFIG_COLOR) && config.has(Constants.CONFIG_DEVICES)) {

            JSONArray listen = config.getJSONArray(Constants.CONFIG_LISTEN);

            if (listen.length() > 1) {
                mHost = listen.getString(0);
                mPort = listen.getInt(1);
            }

            mVerbose = config.getBoolean(Constants.CONFIG_VERBOSE);

            mFcColor = new FadecandyColor(config.getJSONObject(Constants.CONFIG_COLOR));

            JSONArray fcDeviceArr = config.getJSONArray(Constants.CONFIG_DEVICES);

            for (int i = 0; i < fcDeviceArr.length(); i++) {
                mFcDevices.add(new FadecandyDevice((JSONObject) fcDeviceArr.get(i)));
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

From source file:com.cordova.photo.CameraLauncher.java

/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArry of arguments for the plugin.
 * @return                  A PluginResult object with a status and message.
 *//*from   w  w w .  j  a  va 2 s  . c o m*/
private boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    this.callbackContext = callbackContext;

    if (action.equals("takePicture")) {
        int srcType = CAMERA;
        int destType = FILE_URI;
        this.saveToPhotoAlbum = false;
        this.targetHeight = 0;
        this.targetWidth = 0;
        this.encodingType = JPEG;
        this.mediaType = PICTURE;
        this.mQuality = 80;

        this.mQuality = args.getInt(0);
        destType = args.getInt(1);
        srcType = args.getInt(2);
        this.targetWidth = args.getInt(3);
        this.targetHeight = args.getInt(4);
        this.encodingType = args.getInt(5);
        this.mediaType = args.getInt(6);
        this.allowEdit = args.getBoolean(7);
        this.correctOrientation = args.getBoolean(8);
        this.saveToPhotoAlbum = args.getBoolean(9);

        // If the user specifies a 0 or smaller width/height
        // make it -1 so later comparisons succeed
        if (this.targetWidth < 1) {
            this.targetWidth = -1;
        }
        if (this.targetHeight < 1) {
            this.targetHeight = -1;
        }

        try {
            if (srcType == CAMERA) {
                this.takePicture(destType, encodingType);
            } else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
                this.getImage(srcType, destType, encodingType);
            }
        } catch (IllegalArgumentException e) {
            return true;
        }

        return true;
    }
    return false;
}

From source file:com.googlecode.android_scripting.facade.ui.AlertDialogTask.java

/**
 * Set multi choice items./*from   w  w  w . j a  va  2 s .  c om*/
 * 
 * @param items
 *          a list of items as {@link String}s to display
 * @param selected
 *          a list of indices for items that should be selected by default
 * @throws JSONException
 */
public void setMultiChoiceItems(JSONArray items, JSONArray selected) throws JSONException {
    setItems(items);
    mSelectedItems.clear();
    if (selected != null) {
        for (int i = 0; i < selected.length(); i++) {
            mSelectedItems.add(selected.getInt(i));
        }
    }
    mInputType = InputType.MULTI_CHOICE;
}

From source file:io.strider.camera.CameraLauncher.java

/**
 * Executes the request and returns PluginResult.
 *
 * @param action           The action to execute.
 * @param args             JSONArry of arguments for the plugin.
 * @param callbackContext   The callback id used when calling back into JavaScript.
 * @return                 A PluginResult object with a status and message.
 *//*from  w  w w  .j a v  a  2  s.com*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    this.callbackContext = callbackContext;

    if (action.equals("takePicture")) {
        int srcType = CAMERA;
        int destType = FILE_URI;
        this.saveToPhotoAlbum = false;
        this.targetHeight = 0;
        this.targetWidth = 0;
        this.encodingType = JPEG;
        this.mediaType = PICTURE;
        this.mQuality = 80;

        this.mQuality = args.getInt(0);
        destType = args.getInt(1);
        srcType = args.getInt(2);
        this.targetWidth = args.getInt(3);
        this.targetHeight = args.getInt(4);
        this.encodingType = args.getInt(5);
        this.mediaType = args.getInt(6);
        //this.allowEdit = args.getBoolean(7); // This field is unused.
        this.correctOrientation = args.getBoolean(8);
        this.saveToPhotoAlbum = args.getBoolean(9);

        // If the user specifies a 0 or smaller width/height
        // make it -1 so later comparisons succeed
        if (this.targetWidth < 1) {
            this.targetWidth = -1;
        }
        if (this.targetHeight < 1) {
            this.targetHeight = -1;
        }

        try {
            if (srcType == CAMERA) {
                this.cleanCache();
                this.takePicture(destType, encodingType);
            }
            //            else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
            //               this.getImage(srcType, destType);
            //            }
        } catch (IllegalArgumentException e) {
            callbackContext.error("Illegal Argument Exception");
            PluginResult r = new PluginResult(PluginResult.Status.ERROR);
            callbackContext.sendPluginResult(r);
            return true;
        }

        PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
        r.setKeepCallback(true);
        callbackContext.sendPluginResult(r);

        return true;

    } else if (action.equals("cleanup")) {
        try {
            this.cleanCache();
        } catch (IllegalArgumentException e) {
            callbackContext.error("Illegal Argument Exception");
            PluginResult r = new PluginResult(PluginResult.Status.ERROR);
            callbackContext.sendPluginResult(r);
            return true;
        }

        PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
        r.setKeepCallback(true);
        callbackContext.sendPluginResult(r);

        return true;
    }

    return false;
}

From source file:fr.bmartel.android.fadecandy.model.FadecandyDevice.java

public FadecandyDevice(JSONObject fadecandyDevice) {

    try {//from ww  w . j  a  v  a  2  s.  com
        if (fadecandyDevice.has(Constants.CONFIG_TYPE) && fadecandyDevice.has(Constants.CONFIG_MAP)) {

            mType = fadecandyDevice.getString(Constants.CONFIG_TYPE);
            JSONArray map = fadecandyDevice.getJSONArray(Constants.CONFIG_MAP);

            for (int i = 0; i < map.length(); i++) {

                JSONArray mapItem = map.getJSONArray(i);
                List<Integer> itemList = new ArrayList<>();
                for (int j = 0; j < mapItem.length(); j++) {
                    itemList.add(mapItem.getInt(j));
                }

                mMap.add(itemList);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.polychrom.cordova.AccountManagerPlugin.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (manager == null) {
        manager = AccountManager.get(cordova.getActivity());
    }//from   w  ww  .  j  av a2  s .c om

    try {
        if ("getAccountsByType".equals(action)) {
            Account[] account_list = manager.getAccountsByType(args.isNull(0) ? null : args.getString(0));
            JSONArray result = new JSONArray();

            for (Account account : account_list) {
                Integer index = indexForAccount(account);
                accounts.put(index, account);

                JSONObject account_object = new JSONObject();
                account_object.put("_index", (int) index);
                account_object.put("name", account.name);
                account_object.put("type", account.type);
                result.put(account_object);
            }

            callbackContext.success(result);
            return true;
        } else if ("addAccountExplicitly".equals(action)) {
            if (args.isNull(0) || args.getString(0).length() == 0) {
                callbackContext.error("accountType can not be null or empty");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("username can not be null or empty");
                return true;
            } else if (args.isNull(2) || args.getString(2).length() == 0) {
                callbackContext.error("password can not be null or empty");
                return true;
            }

            Account account = new Account(args.getString(1), args.getString(0));
            Integer index = indexForAccount(account);

            Bundle userdata = new Bundle();
            if (!args.isNull(3)) {
                JSONObject userdata_json = args.getJSONObject(3);
                if (userdata_json != null) {
                    Iterator<String> keys = userdata_json.keys();
                    while (keys.hasNext()) {
                        String key = keys.next();
                        userdata.putString(key, userdata_json.getString(key));
                    }
                }
            }

            if (false == manager.addAccountExplicitly(account, args.getString(2), userdata)) {
                callbackContext.error("Account with username already exists!");
                return true;
            }

            accounts.put(index, account);

            JSONObject result = new JSONObject();
            result.put("_index", (int) index);
            result.put("name", account.name);
            result.put("type", account.type);

            callbackContext.success(result);
            return true;
        } else if ("updateCredentials".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            callbackContext.error("Not yet implemented");
            return true;
        } else if ("clearPassword".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.clearPassword(account);
            callbackContext.success();
            return true;
        } else if ("removeAccount".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            int index = args.getInt(0);
            Account account = accounts.get(index);
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            // TODO: Add support for AccountManager (callback)
            AccountManagerFuture<Boolean> future = manager.removeAccount(account, null, null);
            try {
                if (future.getResult() == true) {
                    accounts.remove(index);
                    callbackContext.success();
                } else {
                    callbackContext.error("Failed to remove account");
                }
            } catch (OperationCanceledException e) {
                callbackContext.error("Operation canceled: " + e.getLocalizedMessage());
            } catch (AuthenticatorException e) {
                callbackContext.error("Authenticator error: " + e.getLocalizedMessage());
            } catch (IOException e) {
                callbackContext.error("IO error: " + e.getLocalizedMessage());
            }

            return true;
        } else if ("setAuthToken".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("authTokenType can not be null or empty");
                return true;
            } else if (args.isNull(2) || args.getString(2).length() == 0) {
                callbackContext.error("authToken can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.setAuthToken(account, args.getString(1), args.getString(2));
            callbackContext.success();
            return true;
        } else if ("peekAuthToken".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("authTokenType can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            JSONObject result = new JSONObject();
            result.put("value", manager.peekAuthToken(account, args.getString(1)));
            callbackContext.success(result);
            return true;
        } else if ("getAuthToken".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("authTokenType can not be null or empty");
                return true;
            } else if (args.isNull(3)) {
                callbackContext.error("notifyAuthFailure can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            Bundle options = new Bundle();
            // TODO: Options support (will be relevent when we support AccountManagers)

            // TODO: AccountManager support
            AccountManagerFuture<Bundle> future = manager.getAuthToken(account, args.getString(1), options,
                    args.getBoolean(3), null, null);
            try {
                JSONObject result = new JSONObject();
                result.put("value", future.getResult().getString(AccountManager.KEY_AUTHTOKEN));
                callbackContext.success(result);
            } catch (OperationCanceledException e) {
                callbackContext.error("Operation canceled: " + e.getLocalizedMessage());
            } catch (AuthenticatorException e) {
                callbackContext.error("Authenticator error: " + e.getLocalizedMessage());
            } catch (IOException e) {
                callbackContext.error("IO error: " + e.getLocalizedMessage());
            }

            return true;
        } else if ("setPassword".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("password can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.setPassword(account, args.getString(1));
            callbackContext.success();
            return true;
        } else if ("getPassword".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            JSONObject result = new JSONObject();
            result.put("value", manager.getPassword(account));
            callbackContext.success(result);
            return true;
        } else if ("setUserData".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("key can not be null or empty");
                return true;
            } else if (args.isNull(2) || args.getString(2).length() == 0) {
                callbackContext.error("value can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            manager.setUserData(account, args.getString(1), args.getString(2));
            callbackContext.success();
            return true;
        } else if ("getUserData".equals(action)) {
            if (args.isNull(0)) {
                callbackContext.error("account can not be null");
                return true;
            } else if (args.isNull(1) || args.getString(1).length() == 0) {
                callbackContext.error("key can not be null or empty");
                return true;
            }

            Account account = accounts.get(args.getInt(0));
            if (account == null) {
                callbackContext.error("Invalid account");
                return true;
            }

            JSONObject result = new JSONObject();
            result.put("value", manager.getUserData(account, args.getString(1)));
            callbackContext.success(result);
            return true;
        }
    } catch (SecurityException e) {
        callbackContext.error("Access denied");
        return true;
    }

    return false;
}

From source file:com.intellisol.plugin.Ringer.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (NATIVE_ACTION_STRING.equals(action)) {
        int mode;
        try {//from   w w  w  .jav  a 2  s . c  o m
            // get mode from argument
            mode = args.getInt(0);

            // get context (Android)
            Context ctxt = cordova.getActivity().getBaseContext();
            if (mode == NORMAL) {
                // set ringer mode
                AudioManager audioManager = (AudioManager) ctxt.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            } else if (mode == VIBRATE) {
                AudioManager audioManager = (AudioManager) ctxt.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            } else if (mode == SILENT) {
                AudioManager audioManager = (AudioManager) ctxt.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            }

        } catch (JSONException e) {
            // log error
            Log.d("Ringer", e.toString());
            return false;
        } catch (Exception e) {
            // log error
            Log.d("Ringer", e.toString());
            return false;
        }
    }
    return true;
}

From source file:first.endtoend.GCMIntentService.java

@Override
protected void onMessage(Context context, Intent intent) {

    String message = intent.getStringExtra("message");
    JSONObject json = null;// ww  w .j a v a  2s .c  om

    Log.d(TAG, "RECEIVED A MESSAGE :" + message);

    System.out.println("GCMItentService onMessage :" + message);
    try {
        json = new JSONObject(message);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("notif", json.toString());
        JSONArray array = json.getJSONArray("data");
        int code = json.getInt(Constant.RESPONSE_CODE_KEY);

        switch (code) {
        //add new family
        case Constant.CODE_GCM_1:
            //update existing family
        case Constant.CODE_GCM_2:
            AQueryHelper.load_families(context, params);

            break;

        //delete existing family and all data related to its
        case Constant.CODE_GCM_3:
            if (array.length() > 0)
                for (int i = 0; i < array.length(); i++)
                    AQueryHelper.delete_family(context, array.getInt(i));

            break;

        //add new product
        case Constant.CODE_GCM_4:
            //update existing product
        case Constant.CODE_GCM_5:

            AQueryHelper.load_products(context, params);

            break;

        //delete existing product and all data related to its
        case Constant.CODE_GCM_6:
            if (array.length() > 0)
                for (int i = 0; i < array.length(); i++)
                    AQueryHelper.delete_product(context, array.getInt(i));

            break;

        //add new aid
        case Constant.CODE_GCM_7:

            //update existing aid
        case Constant.CODE_GCM_8:

            AQueryHelper.load_aids(context, params);

            break;

        //delete existing aid and all data related to its
        case Constant.CODE_GCM_9:
            if (array.length() > 0)
                for (int i = 0; i < array.length(); i++)
                    AQueryHelper.delete_aid(context, array.getInt(i));
            break;

        case Constant.CODE_GCM_12:
            try {
                int beneficiaryId = array.getInt(0);
                System.out.println("the id to delete is :" + beneficiaryId);
                BeneficiaryFacade bFacade = new BeneficiaryFacade(context);
                Beneficiary b = bFacade.findById(beneficiaryId);
                if (b != null) {
                    System.out.println(JsonHelper.createJsonObject(b));
                    int familyId = b.getFamily().getFamilyId();
                    List<Integer> list = new ArrayList<Integer>();
                    list.add(familyId);
                    AQueryHelper.delete_beneficiary(context, beneficiaryId);

                    JSONArray jsonArray = new JSONArray(list);
                    Map<String, Object> parameters = new HashMap<String, Object>();
                    JSONObject jo = new JSONObject();
                    jo.put("code", Constant.CODE_GCM_1);
                    jo.put("data", jsonArray);
                    parameters.put("notif", jo.toString());

                    AQueryHelper.load_families(context, parameters);

                } else {
                    System.out.println("No Beneficiary in local with id :" + beneficiaryId);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            break;

        default:
            System.out.println("Code : " + code + " - Non-contracted Code");
            break;
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}