List of usage examples for org.json JSONArray isNull
public boolean isNull(int index)
From source file:com.ezartech.ezar.videooverlay.ezAR.java
private static double getDoubleOrNull(JSONArray args, int i) { if (args.isNull(i)) { return Double.NaN; }/*from ww w . ja v a2s .co m*/ try { return args.getDouble(i); } catch (JSONException e) { Log.e(TAG, "Can't get double", e); throw new RuntimeException(e); } }
From source file:com.xgf.inspection.qrcode.google.zxing.client.result.supplement.BookResultInfoRetriever.java
@Override void retrieveSupplementalInfo() throws IOException { CharSequence contents = HttpHelper.downloadViaHttp( "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn, HttpHelper.ContentType.JSON); if (contents.length() == 0) { return;//from w ww . java 2 s .com } String title; String pages; Collection<String> authors = null; try { JSONObject topLevel = (JSONObject) new JSONTokener(contents.toString()).nextValue(); JSONArray items = topLevel.optJSONArray("items"); if (items == null || items.isNull(0)) { return; } JSONObject volumeInfo = ((JSONObject) items.get(0)).getJSONObject("volumeInfo"); if (volumeInfo == null) { return; } title = volumeInfo.optString("title"); pages = volumeInfo.optString("pageCount"); JSONArray authorsArray = volumeInfo.optJSONArray("authors"); if (authorsArray != null && !authorsArray.isNull(0)) { authors = new ArrayList<String>(authorsArray.length()); for (int i = 0; i < authorsArray.length(); i++) { authors.add(authorsArray.getString(i)); } } } catch (JSONException e) { throw new IOException(e.toString()); } Collection<String> newTexts = new ArrayList<String>(); if (title != null && title.length() > 0) { newTexts.add(title); } if (authors != null && !authors.isEmpty()) { boolean first = true; StringBuilder authorsText = new StringBuilder(); for (String author : authors) { if (first) { first = false; } else { authorsText.append(", "); } authorsText.append(author); } newTexts.add(authorsText.toString()); } if (pages != null && pages.length() > 0) { newTexts.add(pages + "pp."); } String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context) + "/search?tbm=bks&source=zxing&q="; append(isbn, source, newTexts.toArray(new String[newTexts.size()]), baseBookUri + isbn); }
From source file:nl.spellenclubeindhoven.dominionshuffle.LoadSaveActivity.java
public Slot[] getSlotList() { Slot[] result = new Slot[NUM_OF_SLOTS]; int slotCount = 0; try {/* w w w.j av a2 s .c om*/ JSONArray jsonArray = new JSONArray(DataReader.readStringFromFile(this, "slots.json")); for (int i = 0; i < jsonArray.length() && i < NUM_OF_SLOTS; i++) { if (jsonArray.isNull(i)) result[i] = new Slot(); else result[i] = new Slot(jsonArray.getString(i)); slotCount++; } } catch (JSONException ignore) { } for (int i = slotCount; i < NUM_OF_SLOTS; i++) { result[i] = new Slot(); } return result; }
From source file:com.quietlycoding.android.reader.util.api.Tags.java
/** * This method grabs the labels and puts them in an ArrayList to return back * to the caller, basically labels are folders which have different * subscribed feeds in them; See//from w w w. j a va2 s . com * {@link org.androidnerds.feeds.reader.Subscriptions#subscriptionsInLabel} * for how to pull the subscribed feeds for a given label. * * @param sid * the Google Reader authentication string. * @return labels the ArrayList of labels */ public static ArrayList<Label> getLabels(String sid) { final JSONArray tags = pullTags(sid); final int size = tags.length(); final ArrayList<Label> labels = new ArrayList<Label>(size); try { for (int i = 0; i < size; i++) { if (!tags.isNull(i)) { final JSONObject tag = tags.getJSONObject(i); final String id = tag.getString("id"); if (id.contains("label")) { final String label = id.substring(id.lastIndexOf("/") + 1); final Label l = new Label(label, id); l.setSortId(tag.getString("sortid")); labels.add(l); } } } } catch (final Exception e) { Log.d(TAG, "Exception caught:: " + e.toString()); } return labels; }
From source file:de.hscoburg.etif.vbis.lagerix.android.barcode.result.supplement.BookResultInfoRetriever.java
@Override void retrieveSupplementalInfo() throws IOException { CharSequence contents = HttpHelper.downloadViaHttp( "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn, HttpHelper.ContentType.JSON); if (contents.length() == 0) { return;//from w w w . j a va 2 s . c om } String title; String pages; Collection<String> authors = null; try { JSONObject topLevel = (JSONObject) new JSONTokener(contents.toString()).nextValue(); JSONArray items = topLevel.optJSONArray("items"); if (items == null || items.isNull(0)) { return; } JSONObject volumeInfo = ((JSONObject) items.get(0)).getJSONObject("volumeInfo"); if (volumeInfo == null) { return; } title = volumeInfo.optString("title"); pages = volumeInfo.optString("pageCount"); JSONArray authorsArray = volumeInfo.optJSONArray("authors"); if (authorsArray != null && !authorsArray.isNull(0)) { authors = new ArrayList<String>(authorsArray.length()); for (int i = 0; i < authorsArray.length(); i++) { authors.add(authorsArray.getString(i)); } } } catch (JSONException e) { throw new IOException(e.toString()); } Collection<String> newTexts = new ArrayList<String>(); maybeAddText(title, newTexts); maybeAddTextSeries(authors, newTexts); maybeAddText(pages == null || pages.length() == 0 ? null : pages + "pp.", newTexts); String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context) + "/search?tbm=bks&source=zxing&q="; append(isbn, source, newTexts.toArray(new String[newTexts.size()]), baseBookUri + isbn); }
From source file:com.polychrom.cordova.ActionBarPlugin.java
@Override public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException { if (!plugin_actions.contains(action)) { return false; }//from www. j a v a2 s . co m final Activity ctx = (Activity) cordova; if ("isAvailable".equals(action)) { JSONObject result = new JSONObject(); result.put("value", ctx.getWindow().hasFeature(Window.FEATURE_ACTION_BAR)); callbackContext.success(result); return true; } final ActionBar bar = ctx.getActionBar(); if (bar == null) { Window window = ctx.getWindow(); if (!window.hasFeature(Window.FEATURE_ACTION_BAR)) { callbackContext .error("ActionBar feature not available, Window.FEATURE_ACTION_BAR must be enabled!"); } else { callbackContext.error("Failed to get ActionBar"); } return true; } if (menu == null) { callbackContext.error("Options menu not initialised"); return true; } final StringBuffer error = new StringBuffer(); JSONObject result = new JSONObject(); if ("isShowing".equals(action)) { result.put("value", bar.isShowing()); } else if ("getHeight".equals(action)) { result.put("value", bar.getHeight()); } else if ("getDisplayOptions".equals(action)) { result.put("value", bar.getDisplayOptions()); } else if ("getNavigationMode".equals(action)) { result.put("value", bar.getNavigationMode()); } else if ("getSelectedNavigationItem".equals(action)) { result.put("value", bar.getSelectedNavigationIndex()); } else if ("getSubtitle".equals(action)) { result.put("value", bar.getSubtitle()); } else if ("getTitle".equals(action)) { result.put("value", bar.getTitle()); } else { try { JSONException exception = new Runnable() { public JSONException exception = null; public void run() { try { // This is a bit of a hack (should be specific to the request, not global) bases = new String[] { removeFilename(webView.getOriginalUrl()), removeFilename(webView.getUrl()) }; if ("show".equals(action)) { bar.show(); } else if ("hide".equals(action)) { bar.hide(); } else if ("setMenu".equals(action)) { if (args.isNull(0)) { error.append("menu can not be null"); return; } menu_definition = args.getJSONArray(0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ctx.invalidateOptionsMenu(); } } else if ("setTabs".equals(action)) { if (args.isNull(0)) { error.append("menu can not be null"); return; } bar.removeAllTabs(); tab_callbacks.clear(); if (!buildTabs(bar, args.getJSONArray(0))) { error.append("Invalid tab bar definition"); } } else if ("setDisplayHomeAsUpEnabled".equals(action)) { if (args.isNull(0)) { error.append("showHomeAsUp can not be null"); return; } bar.setDisplayHomeAsUpEnabled(args.getBoolean(0)); } else if ("setDisplayOptions".equals(action)) { if (args.isNull(0)) { error.append("options can not be null"); return; } final int options = args.getInt(0); bar.setDisplayOptions(options); } else if ("setDisplayShowHomeEnabled".equals(action)) { if (args.isNull(0)) { error.append("showHome can not be null"); return; } bar.setDisplayShowHomeEnabled(args.getBoolean(0)); } else if ("setDisplayShowTitleEnabled".equals(action)) { if (args.isNull(0)) { error.append("showTitle can not be null"); return; } bar.setDisplayShowTitleEnabled(args.getBoolean(0)); } else if ("setDisplayUseLogoEnabled".equals(action)) { if (args.isNull(0)) { error.append("useLogo can not be null"); return; } bar.setDisplayUseLogoEnabled(args.getBoolean(0)); } else if ("setHomeButtonEnabled".equals(action)) { if (args.isNull(0)) { error.append("enabled can not be null"); return; } bar.setHomeButtonEnabled(args.getBoolean(0)); } else if ("setIcon".equals(action)) { if (args.isNull(0)) { error.append("icon can not be null"); return; } Drawable drawable = getDrawableForURI(args.getString(0)); bar.setIcon(drawable); } else if ("setListNavigation".equals(action)) { JSONArray items = null; if (args.isNull(0) == false) { items = args.getJSONArray(0); } navigation_adapter.setItems(items); bar.setListNavigationCallbacks(navigation_adapter, navigation_listener); } else if ("setLogo".equals(action)) { if (args.isNull(0)) { error.append("logo can not be null"); return; } Drawable drawable = getDrawableForURI(args.getString(0)); bar.setLogo(drawable); } else if ("setNavigationMode".equals(action)) { if (args.isNull(0)) { error.append("mode can not be null"); return; } final int mode = args.getInt(0); bar.setNavigationMode(mode); } else if ("setSelectedNavigationItem".equals(action)) { if (args.isNull(0)) { error.append("position can not be null"); return; } bar.setSelectedNavigationItem(args.getInt(0)); } else if ("setSubtitle".equals(action)) { if (args.isNull(0)) { error.append("subtitle can not be null"); return; } bar.setSubtitle(args.getString(0)); } else if ("setTitle".equals(action)) { if (args.isNull(0)) { error.append("title can not be null"); return; } bar.setTitle(args.getString(0)); } } catch (JSONException e) { exception = e; } finally { synchronized (this) { this.notify(); } } } // Run task synchronously { synchronized (this) { ctx.runOnUiThread(this); this.wait(); } } }.exception; if (exception != null) { throw exception; } } catch (InterruptedException e) { error.append("Function interrupted on UI thread"); } } if (error.length() == 0) { if (result.length() > 0) { callbackContext.success(result); } else { callbackContext.success(); } } else { callbackContext.error(error.toString()); } return true; }
From source file:curt.android.result.supplement.BookResultInfoRetriever.java
@Override void retrieveSupplementalInfo() throws IOException, InterruptedException { String contents = HttpHelper.downloadViaHttp("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn, HttpHelper.ContentType.JSON); if (contents.length() == 0) { return;//from w w w . j av a 2 s. c o m } String title; String pages; Collection<String> authors = null; try { JSONObject topLevel = (JSONObject) new JSONTokener(contents).nextValue(); JSONArray items = topLevel.optJSONArray("items"); if (items == null || items.isNull(0)) { return; } JSONObject volumeInfo = ((JSONObject) items.get(0)).getJSONObject("volumeInfo"); if (volumeInfo == null) { return; } title = volumeInfo.optString("title"); pages = volumeInfo.optString("pageCount"); JSONArray authorsArray = volumeInfo.optJSONArray("authors"); if (authorsArray != null && !authorsArray.isNull(0)) { authors = new ArrayList<String>(); for (int i = 0; i < authorsArray.length(); i++) { authors.add(authorsArray.getString(i)); } } } catch (JSONException e) { throw new IOException(e.toString()); } Collection<String> newTexts = new ArrayList<String>(); if (title != null && title.length() > 0) { newTexts.add(title); } if (authors != null && !authors.isEmpty()) { boolean first = true; StringBuilder authorsText = new StringBuilder(); for (String author : authors) { if (first) { first = false; } else { authorsText.append(", "); } authorsText.append(author); } newTexts.add(authorsText.toString()); } if (pages != null && pages.length() > 0) { newTexts.add(pages + "pp."); } String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context) + "/search?tbm=bks&source=zxing&q="; append(isbn, source, newTexts.toArray(new String[newTexts.size()]), baseBookUri + isbn); }
From source file:ca.nrc.cadc.xml.JsonInputter.java
private void processJSONArray(String key, JSONArray jsonArray, Element arrayElement, Namespace namespace, List<Namespace> namespaces) throws JSONException { String childTypeName = getListElementMap().get(key); for (int i = 0; i < jsonArray.length(); i++) { if (jsonArray.isNull(i)) { continue; }/* ww w .ja v a 2 s . c o m*/ Element child; if (childTypeName == null) { child = arrayElement; } else { child = new Element(childTypeName, namespace); arrayElement.addContent(child); } Object value = jsonArray.get(i); processObject(childTypeName, value, child, namespace, namespaces); } }
From source file:com.dubsar_dictionary.Dubsar.model.Word.java
@Override public void parseData(Object jsonResponse) throws JSONException { JSONArray response = (JSONArray) jsonResponse; mId = response.getInt(0);/*from w w w . j a v a 2 s. c o m*/ mName = new String(response.getString(1)); setPos(response.getString(2)); setInflections(response.getString(3)); setFreqCnt(response.getInt(5)); JSONArray list = response.getJSONArray(4); mSenses = new ArrayList<Sense>(list.length()); for (int j = 0; j < list.length(); ++j) { JSONArray entry = list.getJSONArray(j); int senseId; String senseName; Sense sense; JSONArray _synonyms = entry.getJSONArray(1); ArrayList<Sense> synonyms = new ArrayList<Sense>(_synonyms.length()); for (int k = 0; k < _synonyms.length(); ++k) { JSONArray _synonym = _synonyms.getJSONArray(k); senseId = _synonym.getInt(0); senseName = _synonym.getString(1); sense = new Sense(senseId, senseName, getPartOfSpeech()); synonyms.add(sense); } senseId = entry.getInt(0); String gloss = entry.getString(2); sense = new Sense(senseId, gloss, synonyms, this); sense.setLexname(entry.getString(3)); if (!entry.isNull(4)) { sense.setMarker(entry.getString(4)); } sense.setFreqCnt(entry.getInt(5)); mSenses.add(sense); } }
From source file:ac.robinson.ticqr.TicQRActivity.java
@Override protected void onPageIdFound(String id) { // Toast.makeText(TicQRActivity.this, "Page ID found", Toast.LENGTH_SHORT).show(); RequestParams params = new RequestParams("lookup", id); new AsyncHttpClient().get(SERVER_URL, params, new JsonHttpResponseHandler() { private void handleFailure(int reason) { // TODO: there are concurrency issues here with hiding the progress bar and showing the rescan button // TODO: (e.g., this task and photo taking complete in different orders) findViewById(R.id.parse_progress).setVisibility(View.GONE); getSupportActionBar().setTitle(R.string.title_activity_image_only); supportInvalidateOptionsMenu(); Toast.makeText(TicQRActivity.this, getString(reason), Toast.LENGTH_SHORT).show(); }/*from w w w . jav a 2 s . com*/ @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { try { if ("ok".equals(response.getString("status"))) { mDestinationEmail = response.isNull("destination") ? null : response.getString("destination"); JSONArray boxes = response.getJSONArray("tickBoxes"); if (boxes != null && !boxes.isNull(0)) { for (int i = 0; i < boxes.length(); i++) { JSONObject jsonBox = boxes.getJSONObject(i); TickBoxHolder box = new TickBoxHolder( new PointF(jsonBox.getInt("x"), jsonBox.getInt("y")), jsonBox.getString("description"), jsonBox.getInt("quantity")); box.ticked = true; // first we assume all boxes are ticked box.foundOnImage = false; // (but not yet found on the image) mServerTickBoxes.add(box); } } mBoxesLoaded = true; if (mImageParsed) { verifyBoxes(); } } else { handleFailure(R.string.hint_json_error); } } catch (JSONException e) { handleFailure(R.string.hint_json_error); } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { handleFailure(R.string.hint_connection_error); } }); }