@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); } }
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; }
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; }
@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; }
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++); }
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(); } }
private void readMessage(JsonReader reader) throws IOException { String name = null; String zone = null; String apn = null; String address = null; reader.beginObject(); while (reader.hasNext()) { tag = reader.nextName(); if (tag.equals("name")) { name = reader.nextString(); } else if (tag.equals("zone")) { zone = reader.nextString(); } else if (tag.equals("apn")) { apn = reader.nextString(); } else if (tag.equals("address")) { address = reader.nextString(); } else if (tag.equals("feature") && reader.peek() != JsonToken.NULL) { polygonPath = readLatLngArray(reader); } else { reader.skipValue(); } } // while reader.endObject(); // create new marker and polygon if (address != null && !address.isEmpty()) { address = address + ", COSTA MESA, CA"; } polyopts = new PolygonOptions().addAll(polygonPath); polyopts.fillColor(randomColor()); polyopts.strokeColor(Color.YELLOW); polyopts.strokeWidth(1); polygon = map.addPolygon(polyopts); marker = map.addMarker( new MarkerOptions() .position(polygon.getPoints().get(0)) .title(address) .snippet("APN :[" + apn + "] Zone : (" + zone + ") ") .anchor(0.5f, 0.5f) .infoWindowAnchor(0.5f, 0.5f) .icon(BitmapDescriptorFactory.fromResource(R.drawable.house2))); marker.setVisible(false); }
/** 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(); }
@SuppressWarnings("cast") public static some.test.Simple createUsingJsonStream(Realm realm, JsonReader reader) throws IOException { some.test.Simple obj = realm.createObject(some.test.Simple.class); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("name")) { if (reader.peek() == JsonToken.NULL) { reader.skipValue(); ((SimpleRealmProxyInterface) obj).realmSet$name(null); } else { ((SimpleRealmProxyInterface) obj).realmSet$name((String) reader.nextString()); } } else if (name.equals("age")) { if (reader.peek() == JsonToken.NULL) { reader.skipValue(); throw new IllegalArgumentException("Trying to set non-nullable field 'age' to null."); } else { ((SimpleRealmProxyInterface) obj).realmSet$age((int) reader.nextInt()); } } else { reader.skipValue(); } } reader.endObject(); return obj; }
/** * 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(); } }
@Override public void onHttpRequestComplete(Intent intent) { if (mProgressDialog != null && mProgressDialog.isShowing()) { mProgressDialog.dismiss(); } if (intent.getIntExtra(Constants.IntentExtras.REQUEST_ID, -1) == Constants.ApiRequestId.GET_EMOJIS_DATA) { try { String trendingShows = intent.getStringExtra(Constants.IntentExtras.MESSAGE); JsonReader jsonReader = new JsonReader(new StringReader(trendingShows)); jsonReader.beginObject(); while (jsonReader.hasNext()) { String emojisName = jsonReader.nextName(); String emojisImageUrl = jsonReader.nextString(); Log.d(getTag(), emojisName + ":" + emojisImageUrl); EmojiData emojiData = new EmojiData(); emojiData.setEmojisName(emojisName); emojiData.setEmojisImageUrl(emojisImageUrl); emojiDataList.add(emojiData); } jsonReader.endObject(); this.emojisListAdapter.notifyDataSetChanged(); } catch (IOException e) { e.printStackTrace(); Log.e(getTag(), e.getMessage()); } } }
private static String[] getParametersFromIntent(Intent intent) { if (intent == null) { return null; } String[] parameters = intent.getStringArrayExtra("parameters"); if (parameters != null) { return parameters; } String encodedParameters = intent.getStringExtra("encodedParameters"); if (encodedParameters != null) { JsonReader reader = new JsonReader(new StringReader(encodedParameters)); List<String> parametersList = new ArrayList<String>(); try { reader.beginArray(); while (reader.hasNext()) { parametersList.add(reader.nextString()); } reader.endArray(); reader.close(); return parametersList.toArray(new String[parametersList.size()]); } catch (IOException e) { Log.w(TAG, e.getMessage(), e); } } return null; }
private void parseWeatherFormSys(JsonReader reader) throws IOException { Log.d(TAG, "Entering parseWeatherFormSys"); reader.beginObject(); try { while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case JsonWeatherTokens.COUNTRY: weatherData.setCountry(reader.nextString()); break; case JsonWeatherTokens.SUNRISE: weatherData.setSunrise(reader.nextLong()); break; case JsonWeatherTokens.SUNSET: weatherData.setSunset(reader.nextLong()); break; default: reader.skipValue(); break; } } } finally { reader.endObject(); } }
private void parseWeatherServiceResults(JsonReader reader) throws IOException { Log.d(TAG, "Entering parse Weather Service Results"); reader.beginObject(); try { while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case JsonWeatherTokens.SYS: if (reader.peek() == JsonToken.BEGIN_OBJECT) { parseWeatherFormSys(reader); break; } case JsonWeatherTokens.WEATHER: parseWeatherFormWeather(reader); break; case JsonWeatherTokens.MAIN: parseWeatherFormMain(reader); break; case JsonWeatherTokens.WIND: parseWeatherFormWind(reader); break; case JsonWeatherTokens.NAME: weatherData.setCity(reader.nextString()); break; default: reader.skipValue(); break; } } } finally { reader.endObject(); } }
public void ReadOwnerMessage(JsonReader reader) throws IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("login")) { repo.setOwner_name(reader.nextString()); } else if (name.equals("html_url")) { repo.setOwner_url(reader.nextString()); } else { reader.skipValue(); } } reader.endObject(); }
public Msg readMsg(JsonReader reader) throws IOException { String link = null; String text = null; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("link".equals(name)) { link = reader.nextString(); } else if ("text".equals(name)) { text = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); return new Msg(link, text); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public static final List<Place> GetPlaces(String source) throws IOException { ArrayList<Place> places = new ArrayList<Place>(); JsonReader reader = null; try { reader = new JsonReader(new StringReader(source)); reader.beginArray(); while (reader.hasNext()) { String Code = null; String Name = null; String HubCode = null; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("Code")) { Code = reader.nextString(); } else if (name.equals("Name")) { Name = reader.nextString(); } else if (name.equals("HubCode")) { HubCode = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); Place place = new Place(); place.Code = Code; place.Name = Name; place.HubCode = HubCode; places.add(place); } reader.endArray(); } finally { if (reader != null) reader.close(); } return places; }
public static void read() { String filename = String.format(getRootDir() + "/conf.json"); try { FileInputStream fin = new FileInputStream(filename); JsonReader reader = new JsonReader(new InputStreamReader(fin, "UTF-8")); reader.beginObject(); // write common information while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("LENGTH_UNIT")) { LENGTH_UNIT = reader.nextString(); } else if (name.equals("MAP_TYPE")) { MAP_TYPE = reader.nextString(); } else if (name.equals("SPEED_TYPE")) { SPEED_TYPE = reader.nextString(); } else if (name.equals("MIN_DISTANCE")) { MIN_DISTANCE = reader.nextInt(); } else if (name.equals("INTERVAL_LOCATION")) { INTERVAL_LOCATION = reader.nextInt(); } else if (name.equals("INTERVAL_LOCATION_FAST")) { INTERVAL_LOCATION_FAST = reader.nextInt(); } else if (name.equals("LOCATION_ACCURACY")) { LOCATION_ACCURACY = reader.nextInt(); } else if (name.equals("SPEED_AVG")) { SPEED_AVG = reader.nextInt(); } else if (name.equals("GPS_ONLY")) { GPS_ONLY = reader.nextBoolean(); } else if (name.equals("GPS_ONLY")) { ACCELERATE_FACTOR = (float) reader.nextDouble(); } else { reader.skipValue(); } } reader.endObject(); reader.close(); fin.close(); } catch (Exception e) { Log.i(TAG, "read conf from file fail."); } }
private void readProperties(JsonReader reader) throws IOException { String s; reader.beginObject(); while (reader.hasNext()) { s = reader.nextName(); if (s.equals("mag")) { magnitude = reader.nextDouble(); } else if (s.equals("place")) { details = reader.nextString(); } else if (s.equals("detail")) { link = reader.nextString(); } else if (s.equals("time")) { date = new Date(Long.parseLong(reader.nextString())); } else { reader.skipValue(); } } reader.endObject(); }
public Button parseButton(JsonReader jsonReader) { Button button = new Button(); try { jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_BUT_TARGET)) { button.setTarget(jsonReader.nextString()); } else if (name.equals(JSON_KEY_BUT_TITLE)) { button.setTitle(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); } catch (IOException e) { } catch (Exception e) { try { jsonReader.beginArray(); jsonReader.beginObject(); while (jsonReader.hasNext()) { String name = jsonReader.nextName(); if (name.equals(JSON_KEY_BUT_TARGET)) { button.setTarget(jsonReader.nextString()); } else if (name.equals(JSON_KEY_BUT_TITLE)) { button.setTitle(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); jsonReader.endArray(); } catch (IOException e2) { } } return button; }
private void jsonToUsuario(JsonReader reader) throws JSONException, IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case "id": this.id = reader.nextInt(); break; case "token": this.token = reader.nextString(); break; case "latitud": this.latitud = reader.nextDouble(); break; case "longitud": this.longitud = reader.nextDouble(); break; case "activo": this.activo = reader.nextBoolean(); break; case "ofreceTransito": this.ofreceTransito = reader.nextBoolean(); break; case "autopublicar": this.autopublicar = reader.nextBoolean(); break; case "email": this.email = reader.nextString(); break; case "username": this.username = reader.nextString(); break; case "password": this.password = reader.nextString(); break; case "telefono": this.telefono = reader.nextString(); break; case "direccion": this.direccion = reader.nextString(); break; case "foto": if (reader.peek() == JsonToken.NULL) reader.nextNull(); else { this.foto = null; reader.nextString(); // TODO: Completar cuando el post del usuario admita json // this.foto.setImg(Imagen.bytesFromBase64DEFAULT(reader.nextString())); } break; default: reader.skipValue(); break; } } reader.endObject(); }
private List<String> readSource(JsonReader reader) throws IOException { List<String> source = new ArrayList<>(); reader.beginArray(); while (reader.hasNext()) { source.add(reader.nextString()); } reader.endArray(); return source; }
public Image readImage(JsonReader reader) throws IOException { String enddate = null; String url = null; String copyright = null; String copyrightlink = null; String hsh = null; List<Msg> msgList = new ArrayList<>(); Vid vid = null; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("enddate".equals(name)) { enddate = reader.nextString(); } else if ("url".equals(name)) { url = reader.nextString(); } else if ("copyright".equals(name)) { copyright = reader.nextString(); } else if ("copyrightlink".equals(name)) { copyrightlink = reader.nextString(); } else if ("hsh".equals(name)) { hsh = reader.nextString(); } else if ("msg".equals(name) && reader.peek() != JsonToken.NULL) { msgList.addAll(readMsgs(reader)); } else if ("vid".equals(name) && reader.peek() != JsonToken.NULL) { vid = readVid(reader); } else { reader.skipValue(); } } reader.endObject(); return new Image.Builder() .enddate(enddate) .url(url) .copyright(copyright) .copyrightlink(copyrightlink) .hsh(hsh) .msg(msgList) .vid(vid) .build(); }
public void deSerializeRepresentation(JsonReader reader) throws IOException { ArrayList<String[]> al = new ArrayList<String[]>(); reader.beginObject(); while (reader.hasNext()) { String[] kv = {reader.nextName(), reader.nextString()}; al.add(kv); } reader.endObject(); String[][] oldFormat = al.toArray(new String[al.size()][]); deSerializeRepresentation(oldFormat); }
public static final List<NomenclatureItem> GetNomenclatureItems(String source) throws IOException { ArrayList<NomenclatureItem> items = new ArrayList<NomenclatureItem>(); JsonReader reader = null; try { reader = new JsonReader(new StringReader(source)); reader.beginArray(); while (reader.hasNext()) { String Code = null; String Name = null; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("Code")) { Code = reader.nextString(); } else if (name.equals("Name")) { Name = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); NomenclatureItem item = new NomenclatureItem(); item.Code = Code; item.Name = Name; items.add(item); } reader.endArray(); } finally { if (reader != null) reader.close(); } return items; }
public Response(JsonReader jsonReader) { results = new HashMap<String, String>(); try { jsonReader.beginObject(); while (jsonReader.peek() != JsonToken.END_OBJECT) { String label = jsonReader.nextName(); results.put(label, jsonReader.nextString()); } jsonReader.endObject(); } catch (IOException e) { Log.i("RESPONSE :", e.getMessage()); } }
private Vid readVid(JsonReader reader) throws IOException { List<Source> sourceList = new ArrayList<>(); String image = null; String caption = null; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if ("sources".equals(name)) { sourceList.addAll(readSources(reader)); } else if ("caption".equals(name)) { caption = reader.nextString(); } else if ("image".equals(name)) { image = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); return new Vid.Builder().image(image).caption(caption).sources(sourceList).build(); }
private void readSnippetGroup(List<SnippetListItem> listSnippetItems, JsonReader reader) throws IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("recommendation_basis")) { listSnippetItems.add(new SnippetHeader(reader.nextString())); } else if (name.equals("articles")) { readArticlesArray(listSnippetItems, reader); } } reader.endObject(); }
private Category parseCategory(JsonReader reader) throws IOException { String categoryName = "", categoryId = ""; reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); switch (name) { case "CategoryID": categoryId = reader.nextString(); break; case "CategoryName": categoryName = reader.nextString(); break; default: reader.skipValue(); } } reader.endObject(); if (categoryId.equals("-1")) { return null; } return new Category(categoryName, categoryId); }