/** Peeks at the next token and returns a {@link Double} or {@code null} */ Double nextNumberOrNull() throws IOException { if (_reader.peek() == JsonToken.NULL) { _reader.nextNull(); return null; } return _reader.nextDouble(); }
/** * Reads a JSON object for a specific density and populates variables if the density matches * {@code bitmapDensity}. * * @param reader The JsonReader reading the JSON metadata. * @param bitmapDensity The density of the unscaled {@link Bitmap} that was loaded. * @return True if the JSON object being parsed corresponds to bitmapDensity. * @throws IOException */ private boolean parseMetadataForDensity(JsonReader reader, int bitmapDensity) throws IOException { String name = reader.nextName(); assert name.equals("density"); int density = reader.nextInt(); // If this is metadata for a density other than bitmapDensity, skip parsing the rest of the // object. if (density != bitmapDensity) { reader.skipValue(); // Skip width name. reader.skipValue(); // Skip width value. reader.skipValue(); // Skip height name. reader.skipValue(); // Skip height value. reader.skipValue(); // Skip rectangles name. reader.skipValue(); // Skip rectangles array. return false; } name = reader.nextName(); assert name.equals("width"); mSpriteWidth = reader.nextInt(); name = reader.nextName(); assert name.equals("height"); mSpriteHeight = reader.nextInt(); name = reader.nextName(); assert name.equals("rectangles"); parseFrameRectangles(reader); return true; }
private SnippetArticle readArticleDetails(JsonReader reader) throws IOException { String headline = ""; String publisher = ""; String snippets = ""; String url = ""; String thumbnail = ""; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("headline")) { headline = reader.nextString(); } else if (name.equals("publisher")) { publisher = reader.nextString(); } else if (name.equals("snippet")) { snippets = reader.nextString(); } else if (name.equals("url")) { url = reader.nextString(); } else if (name.equals("thumbnail")) { thumbnail = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); return new SnippetsManager.SnippetArticle( headline, publisher, snippets, url, thumbnail, mNumArticles++); }
private void refreshEarthquakes() { // Get the JSON URL url; try { String quakeFeed = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"; url = new URL(quakeFeed); URLConnection connection; connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection) connection; int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream in = httpConnection.getInputStream(); JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); try { // Clear the old earthquakes earthquakes.clear(); readEarthquakes(reader); } finally { reader.close(); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } }
private void findHourlyWeather(final ForecastReport forecastReport, final boolean isSendReponse) { HttpURLConnection urlConnection = null; try { LocationVO locationVO = forecastReport.locationVO; URL url = new URL( String.format( UtilServiceAPIs.API_WUNDERGROUND, locationVO.getCountryCode().equals("US") ? locationVO.getStateCode() : locationVO.getCountryCode(), locationVO.getPlace())); urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); parseHourlyWeather(reader, forecastReport, isSendReponse); reader.close(); in.close(); } catch (MalformedURLException e) { e.printStackTrace(); return; } catch (Exception e) { e.printStackTrace(); } finally { urlConnection.disconnect(); } }
private void readFeaturesArray(JsonReader reader) throws IOException { reader.beginArray(); while (reader.hasNext()) { readErthquake(reader); } reader.endArray(); }
@Override public void onMessage(int instanceId, String message) { StringReader contents = new StringReader(message); JsonReader reader = new JsonReader(contents); int requestId = -1; String cmd = null; String url = null; try { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals(TAG_CMD)) { cmd = reader.nextString(); } else if (name.equals(TAG_REQUEST_ID)) { requestId = reader.nextInt(); } else if (name.equals("url")) { url = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); reader.close(); if (cmd != null && cmd.equals(CMD_REQUEST_SHOW) && requestId >= 0) { handleRequestShow(instanceId, url, requestId); } } catch (IOException e) { Log.d(TAG, "Error: " + e); } }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public Song parseSong(JsonReader jsonReader) { Song song = new Song(); try { jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_TRACKID)) { song.setId(jsonReader.nextLong()); } else if (name.equals(JSON_KEY_ARTISTNAME)) { song.setArtist(jsonReader.nextString()); } else if (name.equals(JSON_KEY_TRACKNAME)) { song.setTitle(jsonReader.nextString()); } else if (name.equals(JSON_KEY_TRACKPRICE)) { song.setPrice(jsonReader.nextDouble()); } else if (name.equals(JSON_KEY_TRACKCOVER)) { song.setCoverURL(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); } catch (IOException e) { } return song; }
/** Parse metric array */ void parseArray() throws IOException { _reader.beginArray(); while (_reader.hasNext()) { parseBody(); } _reader.endArray(); }
/** Peeks at the next token and returns a {@link Boolean} or {@code null} */ Boolean nextBooleanOrNull() throws IOException { if (_reader.peek() == JsonToken.NULL) { _reader.nextNull(); return null; } return _reader.nextBoolean(); }
/** Peeks at the next token and returns a {@link String} or {@code null} */ String nextStringOrNull() throws IOException { if (_reader.peek() == JsonToken.NULL) { _reader.nextNull(); return null; } return _reader.nextString(); }
/** * Parses the raw JSON resource specified by {@code metadataResId}. The JSON is expected to be in * this format: { "apiVersion": <version number (string)>, "densities": [ { "density": <density * (int)>, "width": <unscaled sprite width (int)>, "height": <unscaled sprite height (int)>, * "rectangles": [ [<list of ints for frame 0>], [<list of ints for frame 1>], ...... ] }, { * "density": <density (int)>, "width": <unscaled sprite width (int)>, "height": <unscaled sprite * height (int)>, "rectangles": [ [<list of ints for frame 0>], [<list of ints for frame 1>], * ...... ] }, ...... ] } * * @param metadataResId The id of the raw resource containing JSON to parse. * @param bitmapDensity The density of the unscaled {@link Bitmap} that was loaded. * @param resources A {@link Resources} instance to load assets from. * @throws IOException */ @VisibleForTesting void parseMetadata(int metadataResId, int bitmapDensity, Resources resources) throws IOException { InputStream inputStream = resources.openRawResource(metadataResId); JsonReader reader = new JsonReader(new InputStreamReader(inputStream)); try { reader.beginObject(); // Start reading top-level object. // Check apiVersion. String name = reader.nextName(); assert name.equals("apiVersion"); String version = reader.nextString(); assert version.equals("1.0"); // Parse array of densities. name = reader.nextName(); assert name.equals("densities"); reader.beginArray(); // Start reading array of densities. while (reader.hasNext()) { reader.beginObject(); // Start reading object for this density. boolean foundDensity = parseMetadataForDensity(reader, bitmapDensity); reader.endObject(); // Stop reading object for this density. if (foundDensity) break; } } finally { reader.close(); inputStream.close(); } }
private void readErthquake(JsonReader reader) throws IOException { String s; reader.beginObject(); while (reader.hasNext()) { s = reader.nextName(); if (s.equals("properties")) { readProperties(reader); } else if (s.equals("geometry")) { readGeometry(reader); } else { reader.skipValue(); } } reader.endObject(); final Quake quake = new Quake(date, details, location, magnitude, link); // Process a newly found earthquake handler.post( new Runnable() { public void run() { addNewQuake(quake); } }); }
private void readJsonStream(InputStream in) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); try { readMessagesArray(reader); } finally { reader.close(); } }
private List<SnippetListItem> readJsonStream(InputStream in) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); try { return readRecommendationsArray(reader); } finally { reader.close(); } }
private void readArticlesArray(List<SnippetListItem> listSnippetItems, JsonReader reader) throws IOException { reader.beginArray(); while (reader.hasNext()) { listSnippetItems.add(readArticleDetails(reader)); } reader.endArray(); }
private List<Source> readSources(JsonReader reader) throws IOException { List<Source> sourceList = new ArrayList<>(); reader.beginArray(); while (reader.hasNext()) { sourceList.add(new Source(readSource(reader), null)); } reader.endArray(); return sourceList; }
private List readHitsArray(JsonReader reader) throws IOException { List hits = new ArrayList<FoodResult>(); reader.beginArray(); while (reader.hasNext()) { hits.add(readHitObject(reader)); } reader.endArray(); return hits; }
public ArrayList parse(InputStream in) throws IOException { JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); try { return readMessagesArray(reader); } finally { reader.close(); } }
private List<LatLng> readLatLngArray(JsonReader reader) throws IOException { List<LatLng> latlngpath = new ArrayList(); reader.beginArray(); while (reader.hasNext()) { latlngpath.add(readLatLngPoint(reader)); } reader.endArray(); return latlngpath; }
public List<Image> readImagesArray(JsonReader reader) throws IOException { List<Image> images = new ArrayList<>(); reader.beginArray(); while (reader.hasNext()) { images.add(readImage(reader)); } reader.endArray(); return images; }
public List<Msg> readMsgs(JsonReader reader) throws IOException { List<Msg> msgList = new ArrayList<>(); reader.beginArray(); while (reader.hasNext()) { msgList.add(readMsg(reader)); } reader.endArray(); return msgList; }
private FoodResult readHitObject(JsonReader reader) throws IOException { FoodResult food = new FoodResult(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("_index")) { String index = reader.nextString(); // we currently do nothing with this value } else if (name.equals("_type")) { String type = reader.nextString(); // we currently do nothing with this value } else if (name.equals("_id")) { food.id = reader.nextString(); } else if (name.equals("_score")) { double score = reader.nextDouble(); // we currently do nothing with this value } else if (name.equals("fields")) { readHitFields(reader, food); } else { reader.skipValue(); } } reader.endObject(); return food; }
public Repo readMessage(JsonReader reader) throws IOException { repo = new Repo(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("name")) { repo.setName(reader.nextString()); } else if (name.equals("description")) { repo.setDescription(reader.nextString()); } else if (name.equals("owner")) { ReadOwnerMessage(reader); } else if (name.equals("html_url")) { repo.setRepo_url(reader.nextString()); } else if (name.equals("fork")) { repo.setFork(reader.nextBoolean()); } else { reader.skipValue(); } } reader.endObject(); return repo; }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public ArrayList<Song> parseSongList(JsonReader jsonReader) { ArrayList<Song> songList = new ArrayList<Song>(); try { jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_RESULTS) && jsonReader.peek() != JsonToken.NULL) { jsonReader.beginArray(); while (jsonReader.hasNext()) { Song song = parseSong(jsonReader); if (song != null) { songList.add(song); } } jsonReader.endArray(); } else { jsonReader.skipValue(); } } jsonReader.endObject(); } catch (IOException e) { } return songList; }
public Promotion parsePromotion(JsonReader jsonReader) { Promotion promo = new Promotion(); try { jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_BUTTON)) { promo.setButton(parseButton(jsonReader)); } else if (name.equals(JSON_KEY_DESCRIPTION)) { promo.setDescription(jsonReader.nextString()); } else if (name.equals(JSON_KEY_FOOTER)) { promo.setFooter(jsonReader.nextString()); } else if (name.equals(JSON_KEY_TITLE)) { promo.setTitle(jsonReader.nextString()); } else if (name.equals(JSON_KEY_IMAGE)) { promo.setImage(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); } catch (IOException e) { } return promo; }
private void parseWeatherFormWeather(JsonReader reader) throws IOException { Log.d(TAG, "Entering parseWeatherFormWeather"); reader.beginArray(); reader.beginObject(); try { while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case JsonWeatherTokens.DESCRIPTION: weatherData.setWeather(reader.nextString()); break; case JsonWeatherTokens.ICON: weatherData.setIcon(reader.nextString()); break; default: reader.skipValue(); break; } } } finally { reader.endObject(); reader.endArray(); } }
public ArrayList<Promotion> parsePromotionList(JsonReader jsonReader) { ArrayList<Promotion> promoList = new ArrayList<Promotion>(); try { jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_PROMOTIONS) && jsonReader.peek() != JsonToken.NULL) { jsonReader.beginArray(); while (jsonReader.hasNext()) { Promotion promo = parsePromotion(jsonReader); if (promo != null) { promoList.add(promo); } } jsonReader.endArray(); } else { jsonReader.skipValue(); } } jsonReader.endObject(); } catch (IOException e) { } return promoList; }
public ArrayList readMessagesArray(JsonReader reader) throws IOException { ArrayList repos = new ArrayList(); reader.beginArray(); while (reader.hasNext()) { repos.add(readMessage(reader)); } reader.endArray(); return repos; }
private List<SnippetListItem> readRecommendationsArray(JsonReader reader) throws IOException { List<SnippetListItem> listSnippetItems = new ArrayList<SnippetListItem>(); mNumArticles = 0; reader.beginArray(); while (reader.hasNext()) { readSnippetGroup(listSnippetItems, reader); } reader.endArray(); RecordHistogram.recordSparseSlowlyHistogram("NewTabPage.Snippets.NumArticles", mNumArticles); return listSnippetItems; }