List of usage examples for org.json JSONArray getString
public String getString(int index) throws JSONException
From source file:org.vaadin.addons.locationtextfield.GoogleGeocoder.java
protected Collection<GeocodedLocation> createLocations(String address, String input) throws GeocodingException { final Set<GeocodedLocation> locations = new LinkedHashSet<GeocodedLocation>(); try {/* w ww . j av a 2 s . c o m*/ JSONObject obj = new JSONObject(input); if ("OK".equals(obj.getString("status"))) { JSONArray results = obj.getJSONArray("results"); boolean ambiguous = results.length() > 1; int limit = results.length(); if (this.getLimit() > 0) limit = Math.min(this.getLimit(), limit); for (int i = 0; i < limit; i++) { JSONObject result = results.getJSONObject(i); GeocodedLocation loc = new GeocodedLocation(); loc.setAmbiguous(ambiguous); loc.setOriginalAddress(address); loc.setGeocodedAddress(result.getString("formatted_address")); JSONArray components = result.getJSONArray("address_components"); for (int j = 0; j < components.length(); j++) { JSONObject component = components.getJSONObject(j); String value = component.getString("short_name"); JSONArray types = component.getJSONArray("types"); for (int k = 0; k < types.length(); k++) { String type = types.getString(k); if ("street_number".equals(type)) loc.setStreetNumber(value); else if ("route".equals(type)) loc.setRoute(value); else if ("locality".equals(type)) loc.setLocality(value); else if ("administrative_area_level_1".equals(type)) loc.setAdministrativeAreaLevel1(value); else if ("administrative_area_level_2".equals(type)) loc.setAdministrativeAreaLevel2(value); else if ("country".equals(type)) loc.setCountry(value); else if ("postal_code".equals(type)) loc.setPostalCode(value); } } JSONObject location = result.getJSONObject("geometry").getJSONObject("location"); loc.setLat(location.getDouble("lat")); loc.setLon(location.getDouble("lng")); loc.setType(getLocationType(result)); locations.add(loc); } } } catch (JSONException e) { throw new GeocodingException(e.getMessage(), e); } return locations; }
From source file:org.vaadin.addons.locationtextfield.GoogleGeocoder.java
private LocationType getLocationType(JSONObject result) throws JSONException { if (!result.has("types")) return LocationType.UNKNOWN; JSONArray types = result.getJSONArray("types"); for (int i = 0; i < types.length(); i++) { final String type = types.getString(i); if ("street_address".equals(type)) return LocationType.STREET_ADDRESS; else if ("route".equals(type)) return LocationType.ROUTE; else if ("intersection".equals(type)) return LocationType.INTERSECTION; else if ("country".equals(type)) return LocationType.COUNTRY; else if ("administrative_area_level_1".equals(type)) return LocationType.ADMIN_LEVEL_1; else if ("administrative_area_level_2".equals(type)) return LocationType.ADMIN_LEVEL_2; else if ("locality".equals(type)) return LocationType.LOCALITY; else if ("neighborhood".equals(type)) return LocationType.NEIGHBORHOOD; else if ("postal_code".equals(type)) return LocationType.POSTAL_CODE; else if ("point_of_interest".equals(type)) return LocationType.POI; }//from w w w .j a va2 s. c o m return LocationType.UNKNOWN; }
From source file:com.remobile.file.FileUtils.java
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) { if (!configured) { callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "File plugin is not configured. Please see the README.md file for details on how to update config.xml")); return true; }/* ww w.ja va 2 s . c o m*/ if (action.equals("testSaveLocationExists")) { threadhelper(new FileOp() { public void run(JSONArray args) { boolean b = DirectoryManager.testSaveLocationExists(); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b)); } }, args, callbackContext); } else if (action.equals("getFreeDiskSpace")) { threadhelper(new FileOp() { public void run(JSONArray args) { long l = DirectoryManager.getFreeDiskSpace(false); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l)); } }, args, callbackContext); } else if (action.equals("testFileExists")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException { String fname = args.getString(0); boolean b = DirectoryManager.testFileExists(fname); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b)); } }, args, callbackContext); } else if (action.equals("testDirectoryExists")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException { String fname = args.getString(0); boolean b = DirectoryManager.testFileExists(fname); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b)); } }, args, callbackContext); } else if (action.equals("readAsText")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, MalformedURLException { String encoding = args.getString(1); int start = args.getInt(2); int end = args.getInt(3); String fname = args.getString(0); readFileAs(fname, start, end, callbackContext, encoding, PluginResult.MESSAGE_TYPE_STRING); } }, args, callbackContext); } else if (action.equals("readAsDataURL")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, MalformedURLException { int start = args.getInt(1); int end = args.getInt(2); String fname = args.getString(0); readFileAs(fname, start, end, callbackContext, null, -1); } }, args, callbackContext); } else if (action.equals("readAsArrayBuffer")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, MalformedURLException { int start = args.getInt(1); int end = args.getInt(2); String fname = args.getString(0); readFileAs(fname, start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_ARRAYBUFFER); } }, args, callbackContext); } else if (action.equals("readAsBinaryString")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, MalformedURLException { int start = args.getInt(1); int end = args.getInt(2); String fname = args.getString(0); readFileAs(fname, start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_BINARYSTRING); } }, args, callbackContext); } else if (action.equals("write")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, FileNotFoundException, IOException, NoModificationAllowedException { String fname = args.getString(0); String data = args.getString(1); int offset = args.getInt(2); Boolean isBinary = args.getBoolean(3); long fileSize = write(fname, data, offset, isBinary); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize)); } }, args, callbackContext); } else if (action.equals("truncate")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, FileNotFoundException, IOException, NoModificationAllowedException { String fname = args.getString(0); int offset = args.getInt(1); long fileSize = truncateFile(fname, offset); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize)); } }, args, callbackContext); } else if (action.equals("requestAllFileSystems")) { threadhelper(new FileOp() { public void run(JSONArray args) throws IOException, JSONException { callbackContext.success(requestAllFileSystems()); } }, args, callbackContext); } else if (action.equals("requestAllPaths")) { cordova.getThreadPool().execute(new Runnable() { public void run() { try { callbackContext.success(requestAllPaths()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } else if (action.equals("requestFileSystem")) { threadhelper(new FileOp() { public void run(JSONArray args) throws IOException, JSONException { int fstype = args.getInt(0); long size = args.optLong(1); if (size != 0 && size > (DirectoryManager.getFreeDiskSpace(true) * 1024)) { callbackContext.sendPluginResult( new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR)); } else { JSONObject obj = requestFileSystem(fstype); callbackContext.success(obj); } } }, args, callbackContext); } else if (action.equals("resolveLocalFileSystemURI")) { threadhelper(new FileOp() { public void run(JSONArray args) throws IOException, JSONException { String fname = args.getString(0); JSONObject obj = resolveLocalFileSystemURI(fname); callbackContext.success(obj); } }, args, callbackContext); } else if (action.equals("getFileMetadata")) { threadhelper(new FileOp() { public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException { String fname = args.getString(0); JSONObject obj = getFileMetadata(fname); callbackContext.success(obj); } }, args, callbackContext); } else if (action.equals("getParent")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, IOException { String fname = args.getString(0); JSONObject obj = getParent(fname); callbackContext.success(obj); } }, args, callbackContext); } else if (action.equals("getDirectory")) { threadhelper(new FileOp() { public void run(JSONArray args) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException { String dirname = args.getString(0); String path = args.getString(1); JSONObject obj = getFile(dirname, path, args.optJSONObject(2), true); callbackContext.success(obj); } }, args, callbackContext); } else if (action.equals("getFile")) { threadhelper(new FileOp() { public void run(JSONArray args) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException { String dirname = args.getString(0); String path = args.getString(1); JSONObject obj = getFile(dirname, path, args.optJSONObject(2), false); callbackContext.success(obj); } }, args, callbackContext); } else if (action.equals("remove")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, NoModificationAllowedException, InvalidModificationException, MalformedURLException { String fname = args.getString(0); boolean success = remove(fname); if (success) { callbackContext.success(); } else { callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR); } } }, args, callbackContext); } else if (action.equals("removeRecursively")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, FileExistsException, MalformedURLException, NoModificationAllowedException { String fname = args.getString(0); boolean success = removeRecursively(fname); if (success) { callbackContext.success(); } else { callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR); } } }, args, callbackContext); } else if (action.equals("moveTo")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException, FileExistsException { String fname = args.getString(0); String newParent = args.getString(1); String newName = args.getString(2); JSONObject entry = transferTo(fname, newParent, newName, true); callbackContext.success(entry); } }, args, callbackContext); } else if (action.equals("copyTo")) { threadhelper(new FileOp() { public void run(JSONArray args) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException, FileExistsException { String fname = args.getString(0); String newParent = args.getString(1); String newName = args.getString(2); JSONObject entry = transferTo(fname, newParent, newName, false); callbackContext.success(entry); } }, args, callbackContext); } else if (action.equals("readEntries")) { threadhelper(new FileOp() { public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException { String fname = args.getString(0); JSONArray entries = readEntries(fname); callbackContext.success(entries); } }, args, callbackContext); } else if (action.equals("_getLocalFilesystemPath")) { // Internal method for testing: Get the on-disk location of a local filesystem url. // [Currently used for testing file-transfer] threadhelper(new FileOp() { public void run(JSONArray args) throws FileNotFoundException, JSONException, MalformedURLException { String localURLstr = args.getString(0); String fname = filesystemPathForURL(localURLstr); callbackContext.success(fname); } }, args, callbackContext); } else { return false; } return true; }
From source file:edu.mit.mobile.android.locast.data.tags.TaggableUtils.java
public static String toFlattenedString(JSONArray ja) throws JSONException { final int len = ja.length(); final StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { if (i > 0) { sb.append(Tag.TAG_DELIM);/*from w w w . jav a 2s . c o m*/ } sb.append(ja.getString(i)); } return sb.toString(); }
From source file:com.ichi2.preferences.StepsPreference.java
/** * Check if the string is a valid format for steps and return that string, reformatted for better usability if * needed.//from ww w. j a va2 s . c o m * * @param steps User input in text editor. * @return The correctly formatted string or null if the input is not valid. */ private String getValidatedStepsInput(String steps) { JSONArray ja = convertToJSON(steps); if (ja == null) { return null; } else { StringBuilder sb = new StringBuilder(); try { for (int i = 0; i < ja.length(); i++) { sb.append(ja.getString(i)).append(" "); } return sb.toString().trim(); } catch (JSONException e) { throw new RuntimeException(e); } } }
From source file:com.ichi2.preferences.StepsPreference.java
/** * Convert steps format.//from ww w .j a v a2 s . c o m * * @param a JSONArray representation of steps. * @return The steps as a space-separated string. */ public static String convertFromJSON(JSONArray a) { StringBuilder sb = new StringBuilder(); try { for (int i = 0; i < a.length(); i++) { sb.append(a.getString(i)).append(" "); } } catch (JSONException e) { throw new RuntimeException(e); } return sb.toString().trim(); }
From source file:de.duenndns.ssl.MemorizingTrustManager.java
private List<String> getPoshFingerprintsFromCache(String domain) { File file = getPoshCacheFile(domain); try {//from w w w.java 2s .c o m InputStream is = new FileInputStream(file); BufferedReader buf = new BufferedReader(new InputStreamReader(is)); String line = buf.readLine(); StringBuilder sb = new StringBuilder(); while (line != null) { sb.append(line).append("\n"); line = buf.readLine(); } JSONObject jsonObject = new JSONObject(sb.toString()); is.close(); long expires = jsonObject.getLong("expires"); long expiresIn = expires - System.currentTimeMillis(); if (expiresIn < 0) { file.delete(); return null; } else { Log.d("mtm", "posh fingerprints expire in " + (expiresIn / 1000) + "s"); } List<String> result = new ArrayList<>(); JSONArray jsonArray = jsonObject.getJSONArray("fingerprints"); for (int i = 0; i < jsonArray.length(); ++i) { result.add(jsonArray.getString(i)); } return result; } catch (FileNotFoundException e) { return null; } catch (IOException e) { return null; } catch (JSONException e) { file.delete(); return null; } }
From source file:com.kevinquan.android.utils.JSONUtils.java
/** * Retrieve a string stored at the provided index from the provided JSON array * @param obj The JSON array to retrieve from * @param index The index to retrieve the string from * @return the string stored at the index, or null if the index doesn't exist *//*ww w . ja va 2s. c o m*/ public static String safeGetStringFromArray(JSONArray obj, int index) { if (obj != null && obj.length() > index) { try { return obj.getString(index); } catch (JSONException e) { Log.w(TAG, "Could not get string from JSONArray from at index " + index, e); } } return null; }
From source file:io.winch.phonegap.plugin.WinchPlugin.java
private void open(CallbackContext callbackContext, JSONArray args) { try {/* ww w.j a va 2 s .c om*/ Context context = this.cordova.getActivity(); String filename = args.getString(0); String ds_id = args.getString(1); String app_secret = args.getString(2); String path = Winch.pathFromFilesDir(context, filename); mWinch = Winch.db(context, path, ds_id, app_secret); callbackContext.success(); } catch (WinchError e) { e.printStackTrace(); PluginResult r = buildErrorResult(e.getErrorCode(), e.getMessage()); callbackContext.sendPluginResult(r); } catch (JSONException e) { e.printStackTrace(); } }
From source file:io.winch.phonegap.plugin.WinchPlugin.java
private void count(CallbackContext callbackContext, JSONArray args) { try {//from www . jav a 2s . c om String namespace = args.getString(0); int count = mWinch.getNamespace(namespace).count(); PluginResult r = new PluginResult(PluginResult.Status.OK, count); callbackContext.sendPluginResult(r); } catch (JSONException e) { e.printStackTrace(); } catch (WinchError e) { e.printStackTrace(); PluginResult r = buildErrorResult(e.getErrorCode(), e.getMessage()); callbackContext.sendPluginResult(r); } }