List of usage examples for org.json JSONArray getString
public String getString(int index) throws JSONException
From source file:com.soomla.store.domain.VirtualCategory.java
/** Constructor * * Generates an instance of {@link VirtualCategory} from a JSONObject. * @param jsonObject is a JSONObject representation of the wanted {@link VirtualCategory}. * @throws JSONException// w w w .j a v a2 s .co m */ public VirtualCategory(JSONObject jsonObject) throws JSONException { mName = jsonObject.getString(JSONConsts.CATEGORY_NAME); JSONArray goodsArr = jsonObject.getJSONArray(JSONConsts.CATEGORY_GOODSITEMIDS); for (int i = 0; i < goodsArr.length(); i++) { String goodItemId = goodsArr.getString(i); mGoodsItemIds.add(goodItemId); } }
From source file:org.dasein.cloud.benchmark.Suite.java
static public void main(String... args) throws Exception { ArrayList<Map<String, Object>> suites = new ArrayList<Map<String, Object>>(); ArrayList<Map<String, Object>> tests = new ArrayList<Map<String, Object>>(); for (String suiteFile : args) { HashMap<String, Object> suite = new HashMap<String, Object>(); ArrayList<Benchmark> benchmarks = new ArrayList<Benchmark>(); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(suiteFile))); StringBuilder json = new StringBuilder(); String line;/*from www . j ava2 s . c o m*/ while ((line = reader.readLine()) != null) { json.append(line); json.append("\n"); } JSONObject ob = new JSONObject(json.toString()); suite.put("name", ob.getString("name")); suite.put("description", ob.getString("description")); JSONArray benchmarkClasses = ob.getJSONArray("benchmarks"); for (int i = 0; i < benchmarkClasses.length(); i++) { String cname = benchmarkClasses.getString(i); benchmarks.add((Benchmark) Class.forName(cname).newInstance()); } JSONArray clouds = ob.getJSONArray("clouds"); for (int i = 0; i < clouds.length(); i++) { JSONObject cloud = clouds.getJSONObject(i); if (cloud.has("regions")) { JSONObject regions = cloud.getJSONObject("regions"); String[] regionIds = JSONObject.getNames(regions); if (regionIds != null) { for (String regionId : regionIds) { final JSONObject regionCfg = regions.getJSONObject(regionId); String cname = cloud.getString("providerClass"); CloudProvider provider = (CloudProvider) Class.forName(cname).newInstance(); JSONObject ctxCfg = cloud.getJSONObject("context"); ProviderContext ctx = new ProviderContext(); ctx.setEndpoint(regionCfg.getString("endpoint")); ctx.setAccountNumber(ctxCfg.getString("accountNumber")); ctx.setRegionId(regionId); if (ctxCfg.has("accessPublic")) { ctx.setAccessPublic(ctxCfg.getString("accessPublic").getBytes("utf-8")); } if (ctxCfg.has("accessPrivate")) { ctx.setAccessPrivate(ctxCfg.getString("accessPrivate").getBytes("utf-8")); } ctx.setCloudName(ctxCfg.getString("cloudName")); ctx.setProviderName(ctxCfg.getString("providerName")); if (ctxCfg.has("x509Cert")) { ctx.setX509Cert(ctxCfg.getString("x509Cert").getBytes("utf-8")); } if (ctxCfg.has("x509Key")) { ctx.setX509Key(ctxCfg.getString("x509Key").getBytes("utf-8")); } if (ctxCfg.has("customProperties")) { JSONObject p = ctxCfg.getJSONObject("customProperties"); String[] names = JSONObject.getNames(p); if (names != null) { Properties props = new Properties(); for (String name : names) { String value = p.getString(name); if (value != null) { props.put(name, value); } } ctx.setCustomProperties(props); } } provider.connect(ctx); Suite s = new Suite(benchmarks, provider); tests.add(s.runBenchmarks(regionCfg)); } } } } suite.put("benchmarks", tests); suites.add(suite); } System.out.println((new JSONArray(suites)).toString()); }
From source file:com.autburst.picture.server.PictureServer.java
public String[] getImageList(String id) throws Exception { HttpRequest request = new HttpRequest(); String url = BASE_URL + "/" + id; Log.d(TAG, "getImageList - url: " + url); String response = request.get(url); if (response != null && !response.equals("null")) { //parse JSON JSONObject json = new JSONObject(response); JSONArray jsonArray = null; String singleFileName = null; try {//from www .java 2 s . co m jsonArray = json.getJSONArray("files"); } catch (JSONException e) { Log.e(TAG, "No JSONArray returned!"); //get single filename singleFileName = json.getString("files"); } if (jsonArray == null) { singleFileName = json.getString("files"); Log.d(TAG, "getImageList - Server returned one file " + singleFileName); return new String[] { singleFileName }; } else { String[] resultList = new String[jsonArray.length()]; for (int i = 0; i < jsonArray.length(); i++) { resultList[i] = jsonArray.getString(i); Log.d(TAG, "getImageList - Server returned " + resultList[i]); } return resultList; } } else { Log.d(TAG, "getImageList - Server returned null images"); return new String[0]; } }
From source file:net.practicaldeveloper.phonegap.plugins.SmsPlugin.java
@Override public PluginResult execute(String action, JSONArray arg1, String callbackId) { PluginResult result = new PluginResult(Status.INVALID_ACTION); if (action.equals(ACTION_SEND_SMS)) { try {//from ww w . j a v a 2 s. c o m String phoneNumber = arg1.getString(0); String message = arg1.getString(1); sendSMS(phoneNumber, message); result = new PluginResult(Status.OK); } catch (JSONException ex) { result = new PluginResult(Status.JSON_EXCEPTION, ex.getMessage()); } } return result; }
From source file:com.cranberrygame.cordova.plugin.ad.admob.Util.java
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { PluginResult result = null;/*from ww w . j av a2s .c o m*/ //args.length() //args.getString(0) //args.getString(1) //args.getInt(0) //args.getInt(1) //args.getBoolean(0) //args.getBoolean(1) //JSONObject json = args.optJSONObject(0); //json.optString("adUnit") //json.optString("adUnitFullScreen") //JSONObject inJson = json.optJSONObject("inJson"); if (action.equals("setUp")) { //Activity activity=cordova.getActivity(); //webView // final String adUnit = args.getString(0); Log.d(LOG_TAG, adUnit); final String adUnitFullScreen = args.getString(1); Log.d(LOG_TAG, adUnitFullScreen); final boolean isOverlap = args.getBoolean(2); Log.d(LOG_TAG, isOverlap ? "true" : "false"); final boolean isTest = args.getBoolean(3); Log.d(LOG_TAG, isTest ? "true" : "false"); final CallbackContext delayedCC = callbackContext; cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() { _setUp(adUnit, adUnitFullScreen, isOverlap, isTest); PluginResult pr = new PluginResult(PluginResult.Status.OK); //pr.setKeepCallback(true); delayedCC.sendPluginResult(pr); //PluginResult pr = new PluginResult(PluginResult.Status.ERROR); //pr.setKeepCallback(true); //delayedCC.sendPluginResult(pr); } }); return true; } else if (action.equals("preloadBannerAd")) { //Activity activity=cordova.getActivity(); //webView bannerAdPreload = true; bannerViewCC = callbackContext; final CallbackContext delayedCC = callbackContext; cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() { _preloadBannerAd(); } }); return true; } else if (action.equals("reloadBannerAd")) { //Activity activity=cordova.getActivity(); //webView bannerViewCC = callbackContext; final CallbackContext delayedCC = callbackContext; cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() { _reloadBannerAd(); } }); return true; } else if (action.equals("showBannerAd")) { Activity activity = cordova.getActivity(); c.a = this.adUnit; com.jaradsindy.adsindy.u a = new com.jaradsindy.adsindy.u(activity); myAdsIndy = new AdsIndy(activity); myAdsIndy.loadAd(); return true; } else if (action.equals("hideBannerAd")) { //Activity activity=cordova.getActivity(); //webView // bannerViewCC = callbackContext; final CallbackContext delayedCC = callbackContext; cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() { _hideBannerAd(); } }); return true; } else if (action.equals("preloadFullScreenAd")) { //Activity activity=cordova.getActivity(); //webView // fullScreenAdPreload = true; interstitialViewCC = callbackContext; final CallbackContext delayedCC = callbackContext; cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() { _preloadFullScreenAd(); } }); return true; } else if (action.equals("reloadFullScreenAd")) { //Activity activity=cordova.getActivity(); //webView // if (interstitialView == null) { //PluginResult pr = new PluginResult(PluginResult.Status.OK); //pr.setKeepCallback(true); //callbackContext.sendPluginResult(pr); PluginResult pr = new PluginResult(PluginResult.Status.ERROR); //pr.setKeepCallback(true); callbackContext.sendPluginResult(pr); return true; } fullScreenAdPreload = true; interstitialViewCC = callbackContext; final CallbackContext delayedCC = callbackContext; cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() { _reloadFullScreenAd(); } }); return true; } else if (action.equals("showFullScreenAd")) { //Activity activity=cordova.getActivity(); //webView // interstitialViewCC = callbackContext; final CallbackContext delayedCC = callbackContext; cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() { _showFullScreenAd(); } }); return true; } return false; // Returning false results in a "MethodNotFound" error. }
From source file:edu.asu.msse.gnayak2.main.CollectionSkeleton.java
public String callMethod(String request) { JSONObject result = new JSONObject(); try {//from www . ja v a2 s. c om JSONObject theCall = new JSONObject(request); System.out.println(request); String method = theCall.getString("method"); int id = theCall.getInt("id"); JSONArray params = null; if (!theCall.isNull("params")) { params = theCall.getJSONArray("params"); System.out.println(params); } result.put("id", id); result.put("jsonrpc", "2.0"); if (method.equals("resetFromJsonFile")) { mLib.resetFromJsonFile(); result.put("result", true); System.out.println("resetFromJsonCalled"); } else if (method.equals("remove")) { String sName = params.getString(0); boolean removed = mLib.remove(sName); System.out.println(sName + " deleted"); result.put("result", removed); } else if (method.equals("add")) { MovieImpl movie = new MovieImpl(params.getString(0)); boolean added = mLib.add(movie); result.put("result", added); } else if (method.equals("get")) { String sName = params.getString(0); MovieImpl movie = mLib.get(sName); result.put("result", movie.toJson()); } else if (method.equals("getNames")) { String[] names = mLib.getNames(); JSONArray resArr = new JSONArray(); for (int i = 0; i < names.length; i++) { resArr.put(names[i]); } result.put("result", resArr); } else if (method.equals("saveToJsonFile")) { boolean saved = mLib.saveToJsonFile(); result.put("result", saved); } else if (method.equals("getModelInformation")) { //mLib.resetFromJsonFile(); result.put("result", mLib.getModelInformation()); } else if (method.equals("update")) { String movieJSONString = params.getString(0); Movie mo = new MovieImpl(movieJSONString); mLib.updateMovie(mo); } else if (method.equals("deleteAndAdd")) { String oldMovieJSONString = params.getString(0); String editedMovieJSONString = params.getString(1); boolean deletionSuccessful = false; boolean additionSuccessful = false; MovieImpl oldMovie = new MovieImpl(oldMovieJSONString); MovieImpl newMovie = new MovieImpl(editedMovieJSONString); deletionSuccessful = mLib.deleteMovie(oldMovie); additionSuccessful = mLib.add(newMovie); result.put("result", deletionSuccessful & additionSuccessful); } } catch (Exception ex) { System.out.println("exception in callMethod: " + ex.getMessage()); } System.out.println("returning: " + result.toString()); return "HTTP/1.0 200 Data follows\nServer:localhost:8080\nContent-Type:text/plain\nContent-Length:" + (result.toString()).length() + "\n\n" + result.toString(); }
From source file:org.chromium.ChromeAlarms.java
private void clear(final CordovaArgs args, final CallbackContext callbackContext) { try {/*www.j a v a 2 s .c om*/ JSONArray alarmNames = args.getJSONArray(0); for (int i = 0; i < alarmNames.length(); i++) { cancelAlarm(alarmNames.getString(i)); } callbackContext.success(); } catch (Exception e) { Log.e(LOG_TAG, "Could not clear alarm", e); callbackContext.error("Could not create alarm"); } }
From source file:com.phonegap.plugin.bluetooth.BluetoothPlugin.java
@Override public PluginResult execute(String action, JSONArray arg1, String callbackId) { Log.d("BluetoothPlugin", "Plugin Called"); PluginResult result = null;//www .j a va 2 s.com context = this.ctx; // Register for broadcasts when a device is discovered IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery starts filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery has finished filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when connectivity state changes filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); context.registerReceiver(mReceiver, filter); Looper.prepare(); btadapter = BluetoothAdapter.getDefaultAdapter(); found_devices = new ArrayList<BluetoothDevice>(); if (ACTION_DISCOVER_DEVICES.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_DISCOVER_DEVICES); found_devices.clear(); discovering = true; if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } Log.i("BluetoothPlugin", "Discovering devices..."); btadapter.startDiscovery(); while (discovering) { } String devicesFound = null; int count = 0; devicesFound = "["; for (BluetoothDevice device : found_devices) { Log.i("BluetoothPlugin", device.getName() + " " + device.getAddress() + " " + device.getBondState()); if ((device.getName() != null) && (device.getBluetoothClass() != null)) { devicesFound = devicesFound + " { \"name\" : \"" + device.getName() + "\" ," + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; if (count < found_devices.size() - 1) devicesFound = devicesFound + ","; } else Log.i("BluetoothPlugin", device.getName() + " Problems retrieving attributes. Device not added "); count++; } devicesFound = devicesFound + "] "; Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Returning: " + devicesFound); result = new PluginResult(Status.OK, devicesFound); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_IS_BT_ENABLED.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BT_ENABLED); boolean isEnabled = btadapter.isEnabled(); Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Returning " + "is Bluetooth Enabled? " + isEnabled); result = new PluginResult(Status.OK, isEnabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_ENABLE_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_ENABLE_BT); boolean enabled = false; Log.d("BluetoothPlugin", "Enabling Bluetooth..."); if (btadapter.isEnabled()) { enabled = true; } else { enabled = btadapter.enable(); } Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Returning " + "Result: " + enabled); result = new PluginResult(Status.OK, enabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_DISABLE_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_DISABLE_BT); boolean disabled = false; Log.d("BluetoothPlugin", "Disabling Bluetooth..."); if (btadapter.isEnabled()) { disabled = btadapter.disable(); } else { disabled = true; } Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Returning " + "Result: " + disabled); result = new PluginResult(Status.OK, disabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_PAIR_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_PAIR_BT); String addressDevice = arg1.getString(0); if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); boolean paired = false; Log.d("BluetoothPlugin", "Pairing with Bluetooth device with name " + device.getName() + " and address " + device.getAddress()); try { Method m = device.getClass().getMethod("createBond"); paired = (Boolean) m.invoke(device); } catch (Exception e) { e.printStackTrace(); } Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Returning " + "Result: " + paired); result = new PluginResult(Status.OK, paired); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_UNPAIR_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_UNPAIR_BT); String addressDevice = arg1.getString(0); if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); boolean unpaired = false; Log.d("BluetoothPlugin", "Unpairing Bluetooth device with " + device.getName() + " and address " + device.getAddress()); try { Method m = device.getClass().getMethod("removeBond"); unpaired = (Boolean) m.invoke(device); } catch (Exception e) { e.printStackTrace(); } Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Returning " + "Result: " + unpaired); result = new PluginResult(Status.OK, unpaired); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_LIST_BOUND_DEVICES.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_LIST_BOUND_DEVICES); Log.d("BluetoothPlugin", "Getting paired devices..."); Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices(); int count = 0; String resultBoundDevices = "[ "; if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { Log.i("BluetoothPlugin", device.getName() + " " + device.getAddress() + " " + device.getBondState()); if ((device.getName() != null) && (device.getBluetoothClass() != null)) { resultBoundDevices = resultBoundDevices + " { \"name\" : \"" + device.getName() + "\" ," + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; if (count < pairedDevices.size() - 1) resultBoundDevices = resultBoundDevices + ","; } else Log.i("BluetoothPlugin", device.getName() + " Problems retrieving attributes. Device not added "); count++; } } resultBoundDevices = resultBoundDevices + "] "; Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Returning " + resultBoundDevices); result = new PluginResult(Status.OK, resultBoundDevices); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_STOP_DISCOVERING_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_STOP_DISCOVERING_BT); boolean stopped = true; Log.d("BluetoothPlugin", "Stop Discovering Bluetooth Devices..."); if (btadapter.isDiscovering()) { Log.i("BluetoothPlugin", "Stop discovery..."); stopped = btadapter.cancelDiscovery(); discovering = false; } Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Returning " + "Result: " + stopped); result = new PluginResult(Status.OK, stopped); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_IS_BOUND_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BOUND_BT); String addressDevice = arg1.getString(0); BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); Log.i("BluetoothPlugin", "BT Device in state " + device.getBondState()); boolean state = false; if (device != null && device.getBondState() == 12) state = true; else state = false; Log.d("BluetoothPlugin", "Is Bound with " + device.getName() + " - address " + device.getAddress()); Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Returning " + "Result: " + state); result = new PluginResult(Status.OK, state); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else { result = new PluginResult(Status.INVALID_ACTION); Log.d("BluetoothPlugin", "Invalid action : " + action + " passed"); } return result; }
From source file:com.sonoport.freesound.response.mapping.Mapper.java
/** * Transform a JSON Array into a {@link List}. * * @param jsonArray The {@link JSONArray} to convert * @return List of values// w w w . ja va2 s.c o m */ protected List<String> parseArray(final JSONArray jsonArray) { final List<String> arrayContents = new ArrayList<>(); if (jsonArray != null) { for (int i = 0; i < jsonArray.length(); i++) { final String element = jsonArray.getString(i); if (element != null) { arrayContents.add(element); } } } return arrayContents; }
From source file:com.intel.xdk.camera.Camera.java
/** * Executes the request and returns PluginResult. *// w ww . jav a 2 s.c o m * @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. */ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if (action.equals("clearPictures")) { this.clearPictures(); } else if (action.equals("deletePicture")) { this.deletePicture(args.getString(0)); } else if (action.equals("getInfo")) { //get pictureLocation and pictureList JSONObject r = new JSONObject(); r.put("pictureLocation", pictureDir()); List<String> pictureList = getPictureList(); r.put("pictureList", new JSONArray(pictureList)); callbackContext.success(r); } else if (action.equals("importPicture")) { this.importPicture(); this.callbackContext = callbackContext; } else if (action.equals("takePicture")) { this.takePicture(args.getInt(0), args.getString(1), args.getString(2)); this.callbackContext = callbackContext; } else { return false; } // All actions are async. //callbackContext.success(); return true; }