Java Code Examples for android.util.JsonReader#beginArray()

The following examples show how to use android.util.JsonReader#beginArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommentsFetcher.java    From NClientV2 with Apache License 2.0 6 votes vote down vote up
private void populateComments() {
    String url=String.format(Locale.US,COMMENT_API_URL,id);
    try {
        Response response=Global.getClient().newCall(new Request.Builder().url(url).build()).execute();
        ResponseBody body=response.body();
        if(body==null){response.close(); return;}
        JsonReader reader=new JsonReader(new InputStreamReader(body.byteStream()));
        reader.beginArray();
        while(reader.hasNext())
            comments.add(new Comment(reader));
        reader.close();
        response.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: GeoMultiPolygon.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void setCoordinatesFromJSONStream(JsonReader reader, int crs) throws IOException {
    setCRS(crs);
    reader.beginArray();
    while (reader.hasNext()){
        GeoPolygon polygon = new GeoPolygon();
        polygon.setCoordinatesFromJSONStream(reader, crs);
        mGeometries.add(polygon);
    }
    reader.endArray();
}
 
Example 3
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
List<DescriptionTerm> readDescriptionTermsArray(JsonReader reader) throws IOException {
    List<DescriptionTerm> terms = new ArrayList<>();

    reader.beginArray();
    while (reader.hasNext()) {
        int offset = -1;
        String value = null;

        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.nextName()) {
                case "offset":
                    offset = reader.nextInt();
                    break;
                case "value":
                    value = reader.nextString();
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();
        terms.add(new DescriptionTerm(offset, value));
    }
    reader.endArray();
    return terms;
}
 
Example 4
Source File: BlocklistProcessor.java    From firefox-echo-show with Mozilla Public License 2.0 5 votes vote down vote up
private static void extractSite(final JsonReader reader, final UrlListCallback callback) throws IOException {
    reader.beginObject();

    final String siteOwner = reader.nextName();
    {
        reader.beginObject();

        while (reader.hasNext()) {
            // We can get the site name using reader.nextName() here:
            reader.skipValue();

            JsonToken nextToken = reader.peek();

            if (nextToken.name().equals("STRING")) {
                // Sometimes there's a "dnt" entry, with unspecified purpose.
                reader.skipValue();
            } else {
                reader.beginArray();

                while (reader.hasNext()) {
                    final String blockURL = reader.nextString();
                    callback.put(blockURL, siteOwner);
                }

                reader.endArray();
            }
        }

        reader.endObject();
    }

    reader.endObject();
}
 
Example 5
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
List<MatchedSubstring> readMatchedSubstringsArray(JsonReader reader) throws IOException {
    List<MatchedSubstring> matchedSubstrings = new ArrayList<>();

    reader.beginArray();
    while (reader.hasNext()) {
        int length = -1;
        int offset = -1;

        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.nextName()) {
                case "length":
                    length = reader.nextInt();
                    break;
                case "offset":
                    offset = reader.nextInt();
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();
        matchedSubstrings.add(new MatchedSubstring(length, offset));
    }
    reader.endArray();
    return matchedSubstrings;
}
 
Example 6
Source File: GeoMultiLineString.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void setCoordinatesFromJSONStream(JsonReader reader, int crs) throws IOException {
    setCRS(crs);
    reader.beginArray();
    while (reader.hasNext()){
        GeoLineString line = new GeoLineString();
        line.setCoordinatesFromJSONStream(reader, crs);
        mGeometries.add(line);
    }
    reader.endArray();
}
 
Example 7
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private static List<ZCashTransactionDetails_taddr> readTxArray(JsonReader reader) throws IOException {
  List<ZCashTransactionDetails_taddr> txs = new LinkedList<>();
  reader.beginArray();
  while (reader.hasNext()) {
    txs.add(readTx(reader));
  }

  reader.endArray();
  return txs;
}
 
Example 8
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private static Vector<ZCashTransactionOutput> readTxInputs(JsonReader reader) throws IOException {
  Vector<ZCashTransactionOutput> vin = new Vector<>();
  reader.beginArray();
  while (reader.hasNext()) {
    vin.add(readTxSingleInput(reader));
  }

  reader.endArray();
  return vin;
}
 
Example 9
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private static Vector<ZCashTransactionOutput> readTxOutputs(JsonReader reader, String txid) throws IOException {
  Vector<ZCashTransactionOutput> vout = new Vector<>();
  //reader.nextName();
  reader.beginArray();
  while (reader.hasNext()) {
    ZCashTransactionOutput out = readTxSingleOutput(reader);
    out.txid = txid;
    vout.add(out);
  }

  reader.endArray();
  return vout;
}
 
Example 10
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
List<AlternativePlaceId> readAltIdsArray(JsonReader reader) throws IOException {
    List<AlternativePlaceId> altIds = new ArrayList<>();

    reader.beginArray();
    while (reader.hasNext()) {
        String placeId = null;
        PlaceScope scope = null;

        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.nextName()) {
                case "place_id":
                    placeId = reader.nextString();
                    break;
                case "scope":
                    scope = readScope(reader);
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();
        altIds.add(new AlternativePlaceId(placeId, scope));
    }
    reader.endArray();
    return altIds;
}
 
Example 11
Source File: BlocklistProcessor.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
private static void extractSite(final JsonReader reader, final UrlListCallback callback) throws IOException {
    reader.beginObject();

    final String siteOwner = reader.nextName();
    {
        reader.beginObject();

        while (reader.hasNext()) {
            // We can get the site name using reader.nextName() here:
            reader.skipValue();

            JsonToken nextToken = reader.peek();

            if (nextToken.name().equals("STRING")) {
                // Sometimes there's a "dnt" entry, with unspecified purpose.
                reader.skipValue();
            } else {
                reader.beginArray();

                while (reader.hasNext()) {
                    final String blockURL = reader.nextString();
                    callback.put(blockURL, siteOwner);
                }

                reader.endArray();
            }
        }

        reader.endObject();
    }

    reader.endObject();
}
 
Example 12
Source File: NGWVectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void readFeatures(JsonReader reader, List<Feature> features) throws IOException, IllegalStateException, NumberFormatException, OutOfMemoryError {
    reader.beginArray();
    while (reader.hasNext()) {
        final Feature feature = NGWUtil.readNGWFeature(reader, getFields(), mCRS);
        if (feature.getGeometry() == null || !feature.getGeometry().isValid())
            continue;
        features.add(feature);
    }
    reader.endArray();
}
 
Example 13
Source File: AndroidPlacesApiJsonParser.java    From android-PlacesAutocompleteTextView with BSD 2-Clause "Simplified" License 5 votes vote down vote up
List<String> readTypesArray(JsonReader reader) throws IOException {
    List<String> types = new ArrayList<>();

    reader.beginArray();
    while (reader.hasNext()) {
        types.add(reader.nextString());
    }
    reader.endArray();
    return types;
}
 
Example 14
Source File: GeoMultiPoint.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void setCoordinatesFromJSONStream(JsonReader reader, int crs) throws IOException {
    setCRS(crs);
    reader.beginArray();
    while (reader.hasNext()){
        GeoPoint pt = new GeoPoint();
        pt.setCoordinatesFromJSONStream(reader, crs);
        mGeometries.add(pt);
    }
    reader.endArray();
}
 
Example 15
Source File: JsonUtils.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
public static List<Object> jsonStrToList(final String s) {
    final ArrayList<Object> list = new ArrayList<>();
    final JsonReader reader = new JsonReader(new StringReader(s));
    try {
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginObject();
            while (reader.hasNext()) {
                final String name = reader.nextName();
                if (name.equals(INTEGER_CLASS_NAME)) {
                    list.add(reader.nextInt());
                } else if (name.equals(STRING_CLASS_NAME)) {
                    list.add(reader.nextString());
                } else {
                    Log.w(TAG, "Invalid name: " + name);
                    reader.skipValue();
                }
            }
            reader.endObject();
        }
        reader.endArray();
        return list;
    } catch (final IOException e) {
    } finally {
        close(reader);
    }
    return Collections.<Object>emptyList();
}
 
Example 16
Source File: SampleChooserActivity.java    From ExoPlayer-Offline with Apache License 2.0 5 votes vote down vote up
private void readSampleGroup(JsonReader reader, List<SampleGroup> groups) throws IOException {
  String groupName = "";
  ArrayList<Sample> samples = new ArrayList<>();

  reader.beginObject();
  while (reader.hasNext()) {
    String name = reader.nextName();
    switch (name) {
      case "name":
        groupName = reader.nextString();
        break;
      case "samples":
        reader.beginArray();
        while (reader.hasNext()) {
          samples.add(readEntry(reader, false));
        }
        reader.endArray();
        break;
      case "offline_samples":
        reader.beginArray();
        while (reader.hasNext()){
          samples.add(readOfflineEntry(reader));
        }
        reader.endArray();
        break;
      case "_comment":
        reader.nextString(); // Ignore.
        break;
      default:
        throw new ParserException("Unsupported name: " + name);
    }
  }
  reader.endObject();

  SampleGroup group = getGroup(groupName, groups);
  group.samples.addAll(samples);
}
 
Example 17
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
private static ZCashTransactionOutput readTxSingleOutput(JsonReader reader) throws IOException {
  ZCashTransactionOutput output = new ZCashTransactionOutput();
  reader.beginObject(); //output
  while (reader.peek() != JsonToken.END_OBJECT) {
    String name = reader.nextName();
    switch (name) {
      case N:
        output.n = reader.nextLong();
        break;
      case SCRIPTPUBKEY:
        reader.beginObject();
        while (reader.peek() != JsonToken.END_OBJECT) {
          name = reader.nextName();
          switch (name) {
            case ADDRESSES:
              reader.beginArray();
              while (reader.hasNext()) {
                output.address = reader.nextString();
              }
              reader.endArray();
              break;
            case ASM:
              output.asm = reader.nextString();
              break;
            case HEX:
              output.hex = reader.nextString();
              break;
            case REQSIGS:
              output.regSigs = reader.nextLong();
              break;
            case TYPE:
              output.type = reader.nextString();
              break;
            default:
              reader.skipValue();
          }
        }
        reader.endObject();
        break;
      case VALUE:
        output.value = Double.valueOf(reader.nextDouble() * 1e8).longValue();
        break;
      case VALUEZAT:
        output.value = reader.nextLong();
        break;
      default:
        reader.skipValue();
    }
  }

  reader.endObject(); //output end
  return output;
}
 
Example 18
Source File: BatchManagerTest.java    From background-geolocation-android with Apache License 2.0 4 votes vote down vote up
private List<BackgroundLocation> readLocationsArray(JsonReader reader) throws IOException {
    List<BackgroundLocation> locations = new ArrayList<BackgroundLocation>();
    reader.beginArray();
    while (reader.hasNext()) {
        BackgroundLocation l = new BackgroundLocation();
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equals("id")) {
                l.setLocationId(reader.nextLong());
            } else if (name.equals("time")) {
                l.setTime(reader.nextLong());
            } else if (name.equals("latitude")) {
                l.setLatitude(reader.nextDouble());
            } else if (name.equals("longitude")) {
                l.setLongitude(reader.nextDouble());
            } else if (name.equals("accuracy")) {
                l.setAccuracy((float)reader.nextDouble());
            } else if (name.equals("speed")) {
                l.setSpeed((float)reader.nextDouble());
            } else if (name.equals("bearing")) {
                l.setBearing((float)reader.nextDouble());
            } else if (name.equals("altitude")) {
                l.setAltitude(reader.nextDouble());
            } else if (name.equals("radius")) {
                JsonToken token = reader.peek();
                if (token != JsonToken.NULL) {
                    l.setRadius((float)reader.nextDouble());
                } else {
                    reader.skipValue();
                }
            } else if (name.equals("provider")) {
                l.setProvider(reader.nextString());
            } else if (name.equals("locationProvider")) {
                l.setLocationProvider(reader.nextInt());
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
        locations.add(l);
    }
    reader.endArray();
    return locations;
}
 
Example 19
Source File: GeoJSONUtil.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void fillLayerFromGeoJSONStream(VectorLayer layer, InputStream in, int srs, IProgressor progressor) throws IOException, NGException {
    int streamSize = in.available();
    if(null != progressor){
        progressor.setIndeterminate(false);
        progressor.setMax(streamSize);
        progressor.setMessage(layer.getContext().getString(R.string.start_fill_layer) + " " + layer.getName());
    }

    SQLiteDatabase db = null;
    if(layer.getFields() != null && layer.getFields().isEmpty()){
        db = DatabaseContext.getDbForLayer(layer);
    }

    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    boolean isWGS84 = srs == GeoConstants.CRS_WGS84;
    long counter = 0;
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if(name.equals(GeoConstants.GEOJSON_TYPE_FEATURES)){
            reader.beginArray();
            while (reader.hasNext()) {
                Feature feature = readGeoJSONFeature(reader, layer, isWGS84);
                if (null != feature) {
                    if(layer.getFields() != null && !layer.getFields().isEmpty()){
                        if (feature.getGeometry() != null)
                            layer.create(feature.getGeometry().getType(), feature.getFields());

                        db = DatabaseContext.getDbForLayer(layer);
                    }

                    if(feature.getGeometry() != null) {
                        layer.createFeatureBatch(feature, db);
                        if(null != progressor){
                            if (progressor.isCanceled()) {
                                layer.save();
                                return;
                            }
                            progressor.setValue(streamSize - in.available());
                            progressor.setMessage(layer.getContext().getString(R.string.process_features) + ": " + counter++);
                        }
                    }
                }
            }
            reader.endArray();
        }
        else {
            reader.skipValue();
        }
    }
    reader.endObject();
    reader.close();

    //if(null != db)
    //    db.close(); // return pragma to init

    layer.save();
}
 
Example 20
Source File: SampleChooserActivity.java    From ExoPlayer-Offline with Apache License 2.0 4 votes vote down vote up
private Sample readEntry(JsonReader reader, boolean insidePlaylist) throws IOException {
  String sampleName = null;
  String uri = null;
  String extension = null;
  UUID drmUuid = null;
  String drmLicenseUrl = null;
  String[] drmKeyRequestProperties = null;
  boolean preferExtensionDecoders = false;
  ArrayList<UriSample> playlistSamples = null;

  reader.beginObject();
  while (reader.hasNext()) {
    String name = reader.nextName();
    switch (name) {
      case "name":
        sampleName = reader.nextString();
        break;
      case "uri":
        uri = reader.nextString();
        break;
      case "extension":
        extension = reader.nextString();
        break;
      case "drm_scheme":
        Assertions.checkState(!insidePlaylist, "Invalid attribute on nested item: drm_scheme");
        drmUuid = getDrmUuid(reader.nextString());
        break;
      case "drm_license_url":
        Assertions.checkState(!insidePlaylist,
            "Invalid attribute on nested item: drm_license_url");
        drmLicenseUrl = reader.nextString();
        break;
      case "drm_key_request_properties":
        Assertions.checkState(!insidePlaylist,
            "Invalid attribute on nested item: drm_key_request_properties");
        ArrayList<String> drmKeyRequestPropertiesList = new ArrayList<>();
        reader.beginObject();
        while (reader.hasNext()) {
          drmKeyRequestPropertiesList.add(reader.nextName());
          drmKeyRequestPropertiesList.add(reader.nextString());
        }
        reader.endObject();
        drmKeyRequestProperties = drmKeyRequestPropertiesList.toArray(new String[0]);
        break;
      case "prefer_extension_decoders":
        Assertions.checkState(!insidePlaylist,
            "Invalid attribute on nested item: prefer_extension_decoders");
        preferExtensionDecoders = reader.nextBoolean();
        break;
      case "playlist":
        Assertions.checkState(!insidePlaylist, "Invalid nesting of playlists");
        playlistSamples = new ArrayList<>();
        reader.beginArray();
        while (reader.hasNext()) {
          playlistSamples.add((UriSample) readEntry(reader, true));
        }
        reader.endArray();
        break;
      default:
        throw new ParserException("Unsupported attribute name: " + name);
    }
  }
  reader.endObject();

  if (playlistSamples != null) {
    UriSample[] playlistSamplesArray = playlistSamples.toArray(
        new UriSample[playlistSamples.size()]);
    return new PlaylistSample(sampleName, drmUuid, drmLicenseUrl, drmKeyRequestProperties,
        preferExtensionDecoders, playlistSamplesArray);
  } else {
    return new UriSample(sampleName, drmUuid, drmLicenseUrl, drmKeyRequestProperties,
        preferExtensionDecoders, uri, extension);
  }
}