List of usage examples for org.json JSONArray getInt
public int getInt(int index) throws JSONException
From source file:com.phonegap.GeoBroker.java
/** * Executes the request and returns PluginResult. * /* w w w . j av a2 s.c o m*/ * @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.Maestro.MidiOptions.java
public static MidiOptions fromJson(String jsonString) { if (jsonString == null) { return null; }/*from ww w . ja v a 2 s . c o m*/ MidiOptions options = new MidiOptions(); try { JSONObject json = new JSONObject(jsonString); JSONArray jsonTracks = json.getJSONArray("tracks"); options.tracks = new boolean[jsonTracks.length()]; for (int i = 0; i < options.tracks.length; i++) { options.tracks[i] = jsonTracks.getBoolean(i); } JSONArray jsonMute = json.getJSONArray("mute"); options.mute = new boolean[jsonMute.length()]; for (int i = 0; i < options.mute.length; i++) { options.mute[i] = jsonMute.getBoolean(i); } JSONArray jsonInstruments = json.getJSONArray("instruments"); options.instruments = new int[jsonInstruments.length()]; for (int i = 0; i < options.instruments.length; i++) { options.instruments[i] = jsonInstruments.getInt(i); } if (json.has("time")) { JSONObject jsonTime = json.getJSONObject("time"); int numer = jsonTime.getInt("numerator"); int denom = jsonTime.getInt("denominator"); int quarter = jsonTime.getInt("quarter"); int tempo = jsonTime.getInt("tempo"); options.time = new TimeSignature(numer, denom, quarter, tempo); } options.useDefaultInstruments = json.getBoolean("useDefaultInstruments"); options.scrollVert = json.getBoolean("scrollVert"); options.showPiano = json.getBoolean("showPiano"); options.showLyrics = json.getBoolean("showLyrics"); options.twoStaffs = json.getBoolean("twoStaffs"); options.showNoteLetters = json.getInt("showNoteLetters"); options.transpose = json.getInt("transpose"); options.key = json.getInt("key"); options.combineInterval = json.getInt("combineInterval"); options.shade1Color = json.getInt("shade1Color"); options.shade2Color = json.getInt("shade2Color"); options.showMeasures = json.getBoolean("showMeasures"); options.playMeasuresInLoop = json.getBoolean("playMeasuresInLoop"); options.playMeasuresInLoopStart = json.getInt("playMeasuresInLoopStart"); options.playMeasuresInLoopEnd = json.getInt("playMeasuresInLoopEnd"); } catch (Exception e) { return null; } return options; }
From source file:com.liferay.mobile.android.v7.mdrrulegroup.MDRRuleGroupService.java
public Integer getRuleGroupsCount(JSONArray groupIds) throws Exception { JSONObject _command = new JSONObject(); try {/*from w ww .ja v a2 s . c om*/ JSONObject _params = new JSONObject(); _params.put("groupIds", checkNull(groupIds)); _command.put("/mdr.mdrrulegroup/get-rule-groups-count", _params); } catch (JSONException _je) { throw new Exception(_je); } JSONArray _result = session.invoke(_command); if (_result == null) { return null; } return _result.getInt(0); }
From source file:com.dubsar_dictionary.Dubsar.model.Search.java
@Override public void parseData(Object jsonResponse) throws JSONException { JSONArray response = (JSONArray) jsonResponse; JSONArray list = response.getJSONArray(1); mTotalPages = response.getInt(2); mResults = new ArrayList<Word>(list.length()); for (int j = 0; j < list.length(); ++j) { JSONArray entry = list.getJSONArray(j); int _id = entry.getInt(0); String name = entry.getString(1); String posString = entry.getString(2); int freqCnt = entry.getInt(3); String otherForms = entry.getString(4); Word word = new Word(_id, name, posString); word.setFreqCnt(freqCnt);//from w w w. j a v a2 s. c o m word.setInflections(otherForms); mResults.add(word); } }
From source file:org.uiautomation.ios.IOSCapabilities.java
public List<Device> getSupportedDevicesFromDeviceFamily() { JSONArray o = (JSONArray) asMap().get(DEVICE_FAMILLY); List<Device> devices = new ArrayList<Device>(); for (int i = 0; i < o.length(); i++) { try {//from ww w.j a v a2 s. c o m devices.add(Device.getFromFamilyCode(o.getInt(i))); } catch (JSONException e) { throw new WebDriverException(o.toString() + " but should contain only 1 or 2."); } } return devices; }
From source file:com.example.android.snake.SnakeMessage.java
public SnakePlayer getPlayer() { try {//from www. j ava 2 s . c o m JSONArray trs = mJSONObject.getJSONArray(KEY_PLAYER_TRAILS); int[] trails = new int[trs.length()]; for (int i = 0; i < trs.length(); i++) { trails[i] = trs.getInt(i); } return new SnakePlayer(mJSONObject.getString(KEY_PLAYER_NAME), trails, mJSONObject.getInt(KEY_PLAYER_COLOR), mJSONObject.getLong(KEY_PLAYER_MOVE_DELAY), mJSONObject.getInt(KEY_PLAYER_DIRECTION)); } catch (JSONException e) { e.printStackTrace(); return null; } }
From source file:com.suarez.cordova.mapsforge.MapsforgePlugin.java
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if ("global-status".equals(action)) { this.status(); callbackContext.success();/* w w w .j a va 2 s . c om*/ return true; } else if (action.contains("native-")) { if ("native-set-center".equals(action)) { try { MapsforgeNative.INSTANCE.setCenter(args.getDouble(0), args.getDouble(1)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-set-zoom".equals(action)) { try { MapsforgeNative.INSTANCE.setZoom(Byte.parseByte(args.getString(0))); callbackContext.success(); } catch (NumberFormatException nfe) { callbackContext.error("Incorrect argument format. Should be: (byte zoom)"); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-show".equals(action)) { try { MapsforgeNative.INSTANCE.show(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-hide".equals(action)) { MapsforgeNative.INSTANCE.hide(); return true; } else if ("native-marker".equals(action)) { try { Activity context = this.cordova.getActivity(); int markerId = context.getResources().getIdentifier(args.getString(0), "drawable", context.getPackageName()); if (markerId == 0) { Log.i(MapsforgePlugin.TAG, "Marker not found...using default marker: marker_green"); markerId = context.getResources().getIdentifier("marker_green", "drawable", context.getPackageName()); } int markerKey = MapsforgeNative.INSTANCE.addMarker(markerId, args.getDouble(1), args.getDouble(2)); callbackContext.success(markerKey); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-polyline".equals(action)) { try { JSONArray points = args.getJSONArray(2); if (points.length() % 2 != 0) throw new JSONException("Invalid array of coordinates. Length should be multiple of 2"); int polylineKey = MapsforgeNative.INSTANCE.addPolyline(args.getInt(0), args.getInt(1), points); callbackContext.success(polylineKey); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-delete-layer".equals(action)) { try { MapsforgeNative.INSTANCE.deleteLayer(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-initialize".equals(action)) { try { MapsforgeNative.createInstance(this.cordova.getActivity(), args.getString(0), args.getInt(1), args.getInt(2)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-set-max-zoom".equals(action)) { try { MapsforgeNative.INSTANCE.setMaxZoom(Byte.parseByte(args.getString(0))); callbackContext.success(); } catch (NumberFormatException nfe) { callbackContext.error("Incorrect argument format. Should be: (byte zoom)"); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-set-min-zoom".equals(action)) { try { MapsforgeNative.INSTANCE.setMinZoom(Byte.parseByte(args.getString(0))); callbackContext.success(); } catch (NumberFormatException nfe) { callbackContext.error("Incorrect argument format. Should be: (byte zoom)"); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-show-controls".equals(action)) { try { MapsforgeNative.INSTANCE.setBuiltInZoomControls(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-clickable".equals(action)) { try { MapsforgeNative.INSTANCE.setClickable(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-show-scale".equals(action)) { try { MapsforgeNative.INSTANCE.showScaleBar(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-destroy-cache".equals(action)) { try { MapsforgeNative.INSTANCE.destroyCache(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } return true; } else if ("native-map-path".equals(action)) { try { MapsforgeNative.INSTANCE.setMapFilePath(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException iae) { callbackContext.error(iae.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-cache-name".equals(action)) { try { MapsforgeNative.INSTANCE.setCacheName(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-theme-path".equals(action)) { try { MapsforgeNative.INSTANCE.setRenderThemePath(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-stop".equals(action)) { try { MapsforgeNative.INSTANCE.onStop(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-start".equals(action)) { try { MapsforgeNative.INSTANCE.onStart(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-destroy".equals(action)) { try { MapsforgeNative.INSTANCE.onDestroy(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-online".equals(action)) { try { MapsforgeNative.INSTANCE.setOnline(args.getString(0), args.getString(1), args.getString(2), args.getString(3), args.getInt(4)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-offline".equals(action)) { try { MapsforgeNative.INSTANCE.setOffline(args.getString(0), args.getString(1)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } } else if (action.contains("cache-")) { if ("cache-get-tile".equals(action)) { try { final long x = args.getLong(0); final long y = args.getLong(1); final byte z = Byte.parseByte(args.getString(2)); final CallbackContext callbacks = callbackContext; cordova.getThreadPool().execute(new Runnable() { public void run() { try { String path = MapsforgeCache.INSTANCE.getTilePath(x, y, z); callbacks.success(path); } catch (IOException e) { callbacks.error(e.getMessage()); } catch (Exception e) { callbacks.error(e.getMessage()); } } }); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (NumberFormatException nfe) { callbackContext.error(nfe.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-initialize".equals(action)) { try { MapsforgeCache.createInstance(cordova.getActivity(), args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-map-path".equals(action)) { try { final String mapFile = args.getString(0); final CallbackContext callbacks = callbackContext; cordova.getThreadPool().execute(new Runnable() { @Override public void run() { try { MapsforgeCache.INSTANCE.setMapFilePath(mapFile); callbacks.success(); } catch (IllegalArgumentException e) { callbacks.error(e.getMessage()); } catch (FileNotFoundException e) { callbacks.error(e.getMessage()); } catch (Exception e) { callbacks.error(e.getMessage()); } } }); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-max-size".equals(action)) { try { MapsforgeCache.INSTANCE.setMaxCacheSize(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-max-age".equals(action)) { try { MapsforgeCache.INSTANCE.setMaxCacheAge(args.getLong(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-cleaning-trigger".equals(action)) { try { MapsforgeCache.INSTANCE.setCleanCacheTrigger(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-enabled".equals(action)) { try { MapsforgeCache.INSTANCE.setCacheEnabled(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-external".equals(action)) { try { MapsforgeCache.INSTANCE.setExternalCache(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-name".equals(action)) { try { MapsforgeCache.INSTANCE.setCacheName(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-tile-size".equals(action)) { try { MapsforgeCache.INSTANCE.setTileSize(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-clean-destroy".equals(action)) { try { MapsforgeCache.INSTANCE.setCleanOnDestroy(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-theme-path".equals(action)) { try { final CallbackContext callbacks = callbackContext; final String themePath = args.getString(0); cordova.getThreadPool().execute(new Runnable() { @Override public void run() { try { MapsforgeCache.INSTANCE.setRenderTheme(themePath); callbacks.success(); } catch (IllegalArgumentException e) { callbacks.error(e.getMessage()); } catch (FileNotFoundException e) { callbacks.error(e.getMessage()); } catch (Exception e) { callbacks.error(e.getMessage()); } } }); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-screen-ratio".equals(action)) { try { MapsforgeCache.INSTANCE.setScreenRatio(Float.parseFloat(args.getString(0))); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (NumberFormatException nfe) { callbackContext.error(nfe.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-overdraw".equals(action)) { try { MapsforgeCache.INSTANCE.setOverdrawFactor(Float.parseFloat(args.getString(0))); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (NumberFormatException nfe) { callbackContext.error(nfe.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-destroy".equals(action)) { try { MapsforgeCache.INSTANCE.onDestroy(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } } return false; // Returning false results in a "MethodNotFound" error. }
From source file:fr.louisbl.cordova.gpslocation.CordovaGPSLocation.java
private void getLastLocation(JSONArray args, CallbackContext callbackContext) { int maximumAge; try {/* ww w . jav a 2 s . com*/ maximumAge = args.getInt(0); } catch (JSONException e) { e.printStackTrace(); maximumAge = 0; } Location last = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // Check if we can use lastKnownLocation to get a quick reading and use // less battery if (last != null && (System.currentTimeMillis() - last.getTime()) <= maximumAge) { PluginResult result = new PluginResult(PluginResult.Status.OK, returnLocationJSON(last)); callbackContext.sendPluginResult(result); } else { getCurrentLocation(callbackContext, Integer.MAX_VALUE); } }
From source file:com.angrystone.JpegCompressor.java
@Override public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String filename = "/mnt/sdcard/DCIM/Camera/Convert_"; String orifilename = "/mnt/sdcard/DCIM/Camera/"; if (action.equals("compress")) { Integer quality = 100;//from w w w . java 2 s . co m try { orifilename = orifilename.concat(args.getString(0)); filename = filename.concat(args.getString(0)); quality = args.getInt(1); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } OutputStream outputStream = null; File file = new File(filename); Bitmap bitmap = BitmapFactory.decodeFile(orifilename); try { outputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); try { outputStream.flush(); outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { status = PluginResult.Status.INVALID_ACTION; } return new PluginResult(status, filename); }
From source file:com.polyvi.xface.extension.messaging.XMessagingExt.java
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { mCallbackContext = callbackContext;// w w w . jav a 2 s . c o m try { if (action.equals(COMMAND_SENDMESSAGE)) { sendMessage(args.getString(0), args.getString(1), args.getString(2), args.getString(3)); } else if (action.equals(COMMAND_GETQUANTITIES)) { int numbers = getQuantities(args.getString(0), args.getString(1)); callbackContext.success(numbers); } else if (action.equals(COMMAND_GETALLMESSAGES)) { JSONArray messages = getAllMessages(args.getString(0), args.getString(1)); callbackContext.success(messages); } else if (action.equals(COMMAND_GETMESSAGE)) { JSONObject message = getMessage(args.getString(0), args.getString(1), args.getInt(2)); callbackContext.success(message); } else if (action.equals(COMMAND_FINDMESSAGES)) { JSONArray messages = findMessages(args.getJSONObject(0), args.getString(1), args.getInt(2), args.getInt(3)); callbackContext.success(messages); } return true; } catch (IllegalArgumentException e) { } catch (Exception e) { } return false; }