List of usage examples for org.json JSONArray getJSONArray
public JSONArray getJSONArray(int index) throws JSONException
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);// w w w. ja v a 2 s.c o m 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); word.setInflections(otherForms); mResults.add(word); } }
From source file:org.dasein.persist.riak.RiakCache.java
@Override public long count(SearchTerm... terms) throws PersistenceException { if (terms == null || terms.length < 1) { return count(); }/*from w ww . j a v a2 s. c om*/ if (wire.isDebugEnabled()) { startCall("count"); } try { String mapFunction = buildMapFunction(true, terms); HashMap<String, Object> request = new HashMap<String, Object>(); HashMap<String, Object> inputs = new HashMap<String, Object>(); terms = matchKeys(inputs, terms); if (inputs.size() < 1) { request.put("inputs", getBucket()); } else { inputs.put("bucket", getBucket()); request.put("inputs", inputs); } ArrayList<Map<String, Object>> query = new ArrayList<Map<String, Object>>(); HashMap<String, Object> maps = new HashMap<String, Object>(); HashMap<String, Object> reduces = new HashMap<String, Object>(); HashMap<String, Object> map = new HashMap<String, Object>(); HashMap<String, Object> reduce = new HashMap<String, Object>(); map.put("language", "javascript"); map.put("source", mapFunction); map.put("keep", true); maps.put("map", map); reduce.put("language", "javascript"); reduce.put("keep", true); reduce.put("name", "Riak.reduceSum"); reduces.put("reduce", reduce); query.add(maps); query.add(reduces); request.put("query", query); String json = (new JSONObject(request)).toString(); HttpClient client = getClient(); PostMethod post = new PostMethod(getEndpoint() + "mapred"); int code; try { post.setRequestEntity(new StringRequestEntity(json, "application/json", "utf-8")); if (wire.isDebugEnabled()) { try { wire.debug(post.getName() + " " + getEndpoint() + "mapred"); wire.debug(""); for (Header h : post.getRequestHeaders()) { wire.debug(h.getName() + ": " + h.getValue()); } wire.debug("Content-length: " + post.getRequestEntity().getContentLength()); wire.debug("Content-type: " + post.getRequestEntity().getContentType()); wire.debug(""); wire.debug(((StringRequestEntity) post.getRequestEntity()).getContent()); wire.debug(""); } catch (Throwable ignore) { // ignore } } code = client.executeMethod(post); } catch (HttpException e) { throw new PersistenceException("HttpException during POST: " + e.getMessage()); } catch (IOException e) { throw new PersistenceException("IOException during POST: " + e.getMessage()); } try { String body = post.getResponseBodyAsString(); try { if (wire.isDebugEnabled()) { wire.debug("----------------------------------------"); wire.debug(""); wire.debug(post.getStatusLine().getStatusCode() + " " + post.getStatusLine().getReasonPhrase()); wire.debug(""); if (body != null) { wire.debug(body); wire.debug(""); } } } catch (Throwable ignore) { // ignore } if (code != HttpStatus.SC_OK) { if (code == HttpStatus.SC_NOT_FOUND) { return 0; } throw new PersistenceException(code + ": " + body); } JSONArray results = new JSONArray(body); return results.getJSONArray(0).getLong(0); } catch (Exception e) { throw new PersistenceException(e); } catch (Throwable t) { throw new PersistenceException(t.getMessage()); } } finally { endCall("count"); } }
From source file:eu.sathra.io.IO.java
private Object getValue(JSONArray array, Class<?> clazz) throws Exception { Object parsedArray = Array.newInstance(clazz, array.length()); for (int c = 0; c < array.length(); ++c) { if ((clazz.equals(String.class) || clazz.isPrimitive()) && !clazz.equals(float.class)) { Array.set(parsedArray, c, array.get(c)); } else if (clazz.equals(float.class)) { Array.set(parsedArray, c, (float) array.getDouble(c)); } else if (clazz.isArray()) { // nested array Array.set(parsedArray, c, getValue(array.getJSONArray(c), float.class)); // TODO } else {//from w ww.ja v a 2s . c om Array.set(parsedArray, c, load(array.getJSONObject(c), clazz)); } } return parsedArray; }
From source file:com.norman0406.slimgress.API.Interface.GameBasket.java
private void processGameEntities(JSONArray gameEntities) throws JSONException { // iterate over game entites for (int i = 0; i < gameEntities.length(); i++) { JSONArray resource = gameEntities.getJSONArray(i); // deserialize the game entity using the JSON representation GameEntityBase newEntity = GameEntityBase.createByJSON(resource); // add the new entity to the world if (newEntity != null) { mGameEntities.add(newEntity); }// www . java 2s .com } }
From source file:com.norman0406.slimgress.API.Interface.GameBasket.java
private void processInventory(JSONArray inventory) throws JSONException { // iterate over inventory items for (int i = 0; i < inventory.length(); i++) { JSONArray resource = inventory.getJSONArray(i); // deserialize the item using the JSON representation ItemBase newItem = ItemBase.createByJSON(resource); // add the new item to the player inventory if (newItem != null) { mInventory.add(newItem);// w w w .j av a 2s. com } } }
From source file:org.apache.cordova.navigationmenu.NavigationMenu.java
/** * Executes the request and returns whether the action was valid. * * @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 if the action was valid, false otherwise. *//*from ww w . j av a 2 s . c om*/ public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException { if (action.equals("initialize")) { if (!this.menuinitialized) { JSONArray menuItems = args.getJSONArray(0); for (int i = 0; i < menuItems.length(); i++) { JSONObject menuItem = menuItems.getJSONObject(i); Integer id = menuItem.getInt("id"); items.put(id, menuItem); } if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { invalidateOptionsMenu(); } callbackContext.success(); } else { callbackContext.error("Menu should be initialized before user pressed on the menu button."); } return true; } else if (action.equals("showPopup")) { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { callbackContext.error("Popup menus requires API at least 11 level"); return true; } JSONArray items = args.getJSONArray(0); this.showPopup(items); callbackContext.success(); return true; } else { return false; } }
From source file:com.piggate.sdk.Piggate.java
public Request RequestGetExchange() { final Request request = new Request(this); request._method = "GET"; //define the request method request._params = null; //define the params request._url = "client/exchange"; //define the url to do the request //Handle the request events (if the request fail or is correct) request._rest_callback = new JsonHttpResponseHandler() { //Handle the request error for JSONArray @Override//from w w w . j a v a 2 s. com public void onFailure(int statusCode, Header[] headers, Throwable e, JSONArray response) { //Unused } //Handle the request error for JSONObject @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject response) { String msg = ""; JSONObject obj = null; // If the response is JSONObject instead of expected JSONArray if (response != null) { try { msg = response.getString("error"); } catch (JSONException a) { } catch (NullPointerException a) { } } if (request._callBack != null) { request._callBack.onError(statusCode, headers, msg, (JSONObject) null); } } //Handle the request success for JSONObject //Return a message to the user @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { String msg = ""; JSONArray obj = null; // If the response is JSONObject instead of expected JSONArray if (response != null) { try { obj = response.getJSONArray("data"); } catch (JSONException a) { } catch (NullPointerException a) { } try { msg = response.getString("success"); } catch (JSONException a) { } catch (NullPointerException a) { } } if (request._callBack != null) { request._callBack.onComplete(statusCode, headers, msg, obj); } } //Handle the request success for JSONArray @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { //Unused } }; return request; }
From source file:com.stockbrowser.search.OpenSearchSearchEngine.java
/** * Queries for a given search term and returns a cursor containing suggestions ordered by best match. *//*from w w w . j a va2 s.com*/ public Cursor getSuggestions(Context context, String query) { if (TextUtils.isEmpty(query)) { return null; } if (!isNetworkConnected(context)) { Log.i(TAG, "Not connected to network."); return null; } String suggestUri = mSearchEngineInfo.getSuggestUriForQuery(query); if (TextUtils.isEmpty(suggestUri)) { // No suggest URI available for this engine return null; } try { String content = readUrl(suggestUri); if (content == null) return null; /* The data format is a JSON array with items being regular strings or JSON arrays * themselves. We are interested in the second and third elements, both of which * should be JSON arrays. The second element/array contains the suggestions and the * third element contains the descriptions. Some search engines don't support * suggestion descriptions so the third element is optional. */ JSONArray results = new JSONArray(content); JSONArray suggestions = results.getJSONArray(1); JSONArray descriptions = null; if (results.length() > 2) { descriptions = results.getJSONArray(2); // Some search engines given an empty array "[]" for descriptions instead of // not including it in the response. if (descriptions.length() == 0) { descriptions = null; } } return new SuggestionsCursor(suggestions, descriptions); } catch (JSONException e) { Log.w(TAG, "Error", e); } 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 ww . j a v a2 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:com.liferay.mobile.android.v62.team.TeamService.java
public JSONArray getGroupTeams(long groupId) throws Exception { JSONObject _command = new JSONObject(); try {/* w ww . j a va 2 s .c o m*/ JSONObject _params = new JSONObject(); _params.put("groupId", groupId); _command.put("/team/get-group-teams", _params); } catch (JSONException _je) { throw new Exception(_je); } JSONArray _result = session.invoke(_command); if (_result == null) { return null; } return _result.getJSONArray(0); }