List of usage examples for org.json JSONArray getString
public String getString(int index) throws JSONException
From source file:net.dv8tion.jda.core.managers.GuildController.java
/** * Creates a new {@link net.dv8tion.jda.core.entities.Emote Emote} in this Guild. * <br>If one or more Roles are specified the new Emote will only be available to Members with any of the specified Roles (see {@link Member#canInteract(Emote)}) * <br>For this to be successful, the logged in account has to have the {@link net.dv8tion.jda.core.Permission#MANAGE_EMOTES MANAGE_EMOTES} Permission. * * <p><b><u>Unicode emojis are not included as {@link net.dv8tion.jda.core.entities.Emote Emote}!</u></b> * <br>Roles may only be available for whitelisted accounts. * * <p>Possible {@link net.dv8tion.jda.core.requests.ErrorResponse ErrorResponses} caused by * the returned {@link net.dv8tion.jda.core.requests.RestAction RestAction} include the following: * <ul>/*from ww w . j av a 2s. c om*/ * <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#MISSING_PERMISSIONS MISSING_PERMISSIONS} * <br>The emote could not be created due to a permission discrepancy</li> * * <li>{@link net.dv8tion.jda.core.requests.ErrorResponse#MISSING_ACCESS MISSING_ACCESS} * <br>We were removed from the Guild before finishing the task</li> * </ul> * * @param name * The name for the new Emote * @param icon * The {@link net.dv8tion.jda.core.entities.Icon} for the new Emote * @param roles * The {@link net.dv8tion.jda.core.entities.Role Roles} the new Emote should be restricted to * <br>If no roles are provided the Emote will be available to all Members of this Guild * * @throws net.dv8tion.jda.core.exceptions.PermissionException * If the logged in account does not have the {@link net.dv8tion.jda.core.Permission#MANAGE_EMOTES MANAGE_EMOTES} Permission * @throws net.dv8tion.jda.core.exceptions.GuildUnavailableException * If the guild is temporarily not {@link net.dv8tion.jda.core.entities.Guild#isAvailable() available} * @throws net.dv8tion.jda.core.exceptions.AccountTypeException * If the logged in account is not from {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT} * * @return {@link net.dv8tion.jda.core.requests.restaction.AuditableRestAction AuditableRestAction} - Type: {@link net.dv8tion.jda.core.entities.Emote Emote} * <br>The newly created Emote */ @CheckReturnValue public AuditableRestAction<Emote> createEmote(String name, Icon icon, Role... roles) { checkAvailable(); checkPermission(Permission.MANAGE_EMOTES); Checks.notNull(name, "emote name"); Checks.notNull(icon, "emote icon"); if (getJDA().getAccountType() != AccountType.CLIENT) throw new AccountTypeException(AccountType.CLIENT); JSONObject body = new JSONObject(); body.put("name", name); body.put("image", icon.getEncoding()); if (roles.length > 0) // making sure none of the provided roles are null before mapping them to the snowflake id body.put("roles", Stream.of(roles).filter(Objects::nonNull).map(ISnowflake::getId).collect(Collectors.toSet())); Route.CompiledRoute route = Route.Emotes.CREATE_EMOTE.compile(guild.getId()); return new AuditableRestAction<Emote>(getJDA(), route, body) { @Override protected void handleResponse(Response response, Request<Emote> request) { if (response.isOk()) { JSONObject obj = response.getObject(); final long id = obj.getLong("id"); String name = obj.getString("name"); EmoteImpl emote = new EmoteImpl(id, guild).setName(name); // managed is false by default, should always be false for emotes created by client accounts. JSONArray rolesArr = obj.getJSONArray("roles"); Set<Role> roleSet = emote.getRoleSet(); for (int i = 0; i < rolesArr.length(); i++) { roleSet.add(guild.getRoleById(rolesArr.getString(i))); } // put emote into cache ((GuildImpl) guild).getEmoteMap().put(id, emote); request.onSuccess(emote); } else request.onFailure(response); } }; }
From source file:com.serenegiant.usb.UVCCamera.java
private static final void addSize(final JSONObject format, final int type, final List<Size> size_list) throws JSONException { final JSONArray size = format.getJSONArray("size"); final int size_nums = size.length(); for (int j = 0; j < size_nums; j++) { final String[] sz = size.getString(j).split("x"); try {/*from w ww . j a v a 2 s . c o m*/ size_list.add(new Size(type, j, Integer.parseInt(sz[0]), Integer.parseInt(sz[1]))); } catch (final Exception e) { break; } } }
From source file:com.nolanlawson.cordova.sqlite.SQLitePlugin.java
private SQLitePLuginResult[] execInBackgroundAndReturnResults(JSONArray args) throws JSONException { String dbName = args.getString(0); JSONArray queries = args.getJSONArray(1); boolean readOnly = args.getBoolean(2); int numQueries = queries.length(); SQLitePLuginResult[] results = new SQLitePLuginResult[numQueries]; SQLiteDatabase db = getDatabase(dbName); for (int i = 0; i < numQueries; i++) { JSONArray sqlQuery = queries.getJSONArray(i); String sql = sqlQuery.getString(0); String[] bindArgs = jsonArrayToStringArray(sqlQuery.getJSONArray(1)); try {/*from www.j a v a 2 s . c o m*/ if (isSelect(sql)) { results[i] = doSelectInBackgroundAndPossiblyThrow(sql, bindArgs, db); } else { // update/insert/delete if (readOnly) { results[i] = new SQLitePLuginResult(EMPTY_ROWS, EMPTY_COLUMNS, 0, 0, new ReadOnlyException()); } else { results[i] = doUpdateInBackgroundAndPossiblyThrow(sql, bindArgs, db); } } } catch (Throwable e) { if (DEBUG_MODE) { e.printStackTrace(); } results[i] = new SQLitePLuginResult(EMPTY_ROWS, EMPTY_COLUMNS, 0, 0, e); } } return results; }
From source file:com.nolanlawson.cordova.sqlite.SQLitePlugin.java
private static String[] jsonArrayToStringArray(JSONArray jsonArray) throws JSONException { int len = jsonArray.length(); String[] res = new String[len]; for (int i = 0; i < len; i++) { res[i] = jsonArray.getString(i); }//from www . j av a2 s . c om return res; }
From source file:com.angrystone.JpegExifReader.java
@Override public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; Integer result = 0;// w w w. ja v a 2s .c om if (action.equals("getWidth")) { String file = null; try { file = args.getString(0); } catch (JSONException e) { e.printStackTrace(); } try { ExifInterface exif = new ExifInterface(file); result = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (action.equals("getLength")) { String file = null; try { file = args.getString(0); } catch (JSONException e) { e.printStackTrace(); } try { ExifInterface exif = new ExifInterface(file); result = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0); } catch (IOException e) { e.printStackTrace(); } } else { status = PluginResult.Status.INVALID_ACTION; } return new PluginResult(status, result); }
From source file:org.liberty.android.fantastischmemo.downloader.DownloaderSS.java
private void downloadDatabase(DownloadItem di) throws Exception { String url = di.getAddress(); String jsonString = downloaderUtils.downloadJSONString(url); JSONObject jsonObject = new JSONObject(jsonString); JSONArray jsonDataArray = jsonObject.getJSONArray("data"); List<Card> cardList = new LinkedList<Card>(); for (int i = 0; i < jsonDataArray.length(); i++) { JSONArray jsonItemArray = jsonDataArray.getJSONArray(i); String question = jsonItemArray.getString(0); String answer = jsonItemArray.getString(1); if (question != null && !question.equals("")) { Card card = new Card(); card.setQuestion(question);/* w ww.java2s . c om*/ card.setAnswer(answer); card.setOrdinal(i + 1); card.setCategory(new Category()); card.setLearningData(new LearningData()); cardList.add(card); } } String dbname = di.getTitle() + ".db"; String dbpath = AMEnv.DEFAULT_ROOT_PATH; String fullpath = dbpath + dbname; AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(DownloaderSS.this, fullpath); try { CardDao cardDao = helper.getCardDao(); cardDao.createCards(cardList); long count = helper.getCardDao().getTotalCount(null); if (count <= 0L) { throw new RuntimeException("Downloaded empty db."); } } finally { AnyMemoDBOpenHelperManager.releaseHelper(helper); } recentListUtil.addToRecentList(fullpath); }
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 w w . ja v 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.clarkparsia.pelletserver.client.utils.PelletServerUtils.java
/** * Parse all the {@link PelletService}s in the {@link PelletServer} root * /* w ww.ja va 2 s . c o m*/ * @param server * The {@link PelletServer} * @param content * The content of the HTTP request * @return the list of services * @throws PelletClientException * if there was an error during invocation */ public static Collection<PelletService> parseServerRootForServices(PelletServer server, String content) throws PelletClientException { List<PelletService> services = Lists.newArrayList(); try { JSONObject root = new JSONObject(content); JSONArray rnames = root.names(); for (int i = 0; i < rnames.length(); i++) { String serviceName = rnames.getString(i); if (!serviceName.equals("knowledge-bases") && !serviceName.equals("server-information")) { PelletService service = parsePelletService(server, root.getJSONObject(serviceName), serviceName); if (service != null) services.add(service); } } } catch (Exception e) { throw new PelletClientException("Problem parsing services from server", e); } return services; }
From source file:com.clarkparsia.pelletserver.client.utils.PelletServerUtils.java
/** * Parse {@link PelletServer} server-information * //from w w w . j a va 2 s .c o m * @param server * the PelletServer instance * @param content * the info from the server to be parsed * @return the parsed information * @throws PelletClientException * if there was an error during invocation */ public static Map<String, String> parseServerInfo(PelletServer server, String content) throws PelletClientException { try { JSONObject root = new JSONObject(content); Map<String, String> info = Maps.newHashMap(); if (root.has("server-information")) { JSONObject obj = root.getJSONObject("server-information"); JSONArray names = obj.names(); for (int i = 0; i < names.length(); i++) { String key = names.getString(i); String value = obj.getString(key); info.put(key, value); } } return info; } catch (Exception e) { throw new PelletClientException("Problem parsing server information", e); } }