android.util.JsonReader#close ( )源码实例Demo

下面列出了android.util.JsonReader#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: NClientV2   文件: CommentsFetcher.java
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();
    }
}
 
源代码2 项目: NClientV2   文件: ScrapeTags.java
private void fetchTags()throws IOException {
    Response x=Global.getClient(this).newCall(new Request.Builder().url(TAGS).build()).execute();
    ResponseBody body=x.body();
    if(body==null){
        x.close();
        return;
    }
    JsonReader reader=new JsonReader(body.charStream());
    reader.beginArray();
    while (reader.hasNext()) {
        Tag tag=readTag(reader);
        Queries.TagTable.insert(tag,true);
    }
    reader.close();
    x.close();
}
 
private TokenResult readGenerateAuthTokenResponse(HttpURLConnection conn) throws IOException {
  InputStream inputStream = conn.getInputStream();
  JsonReader reader = new JsonReader(new InputStreamReader(inputStream, UTF_8));
  TokenResult.Builder builder = TokenResult.builder();
  reader.beginObject();
  while (reader.hasNext()) {
    String name = reader.nextName();
    if (name.equals("token")) {
      builder.setToken(reader.nextString());
    } else if (name.equals("expiresIn")) {
      builder.setTokenExpirationTimestamp(parseTokenExpirationTimestamp(reader.nextString()));
    } else {
      reader.skipValue();
    }
  }
  reader.endObject();
  reader.close();
  inputStream.close();

  return builder.setResponseCode(TokenResult.ResponseCode.OK).build();
}
 
源代码4 项目: firebase-android-sdk   文件: LogResponse.java
@NonNull
public static LogResponse fromJson(@NonNull Reader reader) throws IOException {
  JsonReader jsonReader = new JsonReader(reader);
  try {
    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
      String name = jsonReader.nextName();
      if (name.equals("nextRequestWaitMillis")) {
        if (jsonReader.peek() == JsonToken.STRING) {
          return LogResponse.create(Long.parseLong(jsonReader.nextString()));
        } else {
          return LogResponse.create(jsonReader.nextLong());
        }
      }
      jsonReader.skipValue();
    }
    throw new IOException("Response is missing nextRequestWaitMillis field.");
  } finally {
    jsonReader.close();
  }
}
 
源代码5 项目: pivaa   文件: AboutJSONParser.java
/**
 * Reading JSON stream from InputStream
 * @param in
 * @return
 * @throws IOException
 */
private ArrayList<AboutRecord> readJsonStream(InputStream in) {
    try {
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        try {
            return readMessagesArray(reader);
        } finally {
            reader.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    ArrayList<AboutRecord> list = new ArrayList<AboutRecord>();
    return list;
}
 
源代码6 项目: NClientV2   文件: LoadTags.java
private void analyzeScripts(Elements scripts)throws IOException {
    if (scripts.size() > 0) {
        Login.clearOnlineTags();
        String array=extractArray(scripts.last());
        JsonReader reader = new JsonReader(new StringReader(array));
        readTags(reader);
        reader.close();
    }
}
 
源代码7 项目: guarda-android-wallets   文件: JSONParser.java
public static List<ZCashTransactionDetails_taddr> parseTxArray(InputStream is) throws IOException {
  JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
  List<ZCashTransactionDetails_taddr> txs = null;
  try {
    txs = readTxArray(reader);
    reader.close();
  } catch (IOException e) {
    e.printStackTrace();
    Log.e("Error message", e.getMessage());
  }

  return txs;
}
 
源代码8 项目: guarda-android-wallets   文件: JSONParser.java
public static List<ZCashTransactionDetails_taddr> parseTxArray(InputStream is) throws IOException {
  JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
  List<ZCashTransactionDetails_taddr> txs = null;
  try {
    txs = readTxArray(reader);
    reader.close();
  } catch (IOException e) {
    e.printStackTrace();
    Log.e("Error message", e.getMessage());
  }

  return txs;
}
 
源代码9 项目: Wrox-ProfessionalAndroid-4E   文件: MyViewModel.java
private List<Earthquake> parseJson(InputStream in) throws IOException {
  // Create a new Json Reader to parse the input.
  JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

  try {
    // Create an empty list of earthquakes.
    List<Earthquake> earthquakes = null;

    // The root node of the Earthquake JSON feed is an object that
    // we must parse.
    reader.beginObject();
    while (reader.hasNext()) {
      String name = reader.nextName();

      // We are only interested in one sub-object: the array of
      // earthquakes labeled as features.
      if (name.equals("features")) {
        earthquakes = readEarthquakeArray(reader);
      } else {
        // We will ignore all other root level values and objects.
        reader.skipValue();
      }
    }
    reader.endObject();

    return earthquakes;

  } finally {
    reader.close();
  }
}
 
源代码10 项目: android_maplib   文件: GeoJSONUtil.java
public static boolean readGeoJSONCRS(InputStream is, Context context) throws IOException, NGException {
    JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
    boolean isWGS = true;
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if(name.equals(GeoConstants.GEOJSON_CRS)) {
            reader.beginObject();
            while (reader.hasNext()) {
                name = reader.nextName();
                if(name.equals(GeoConstants.GEOJSON_PROPERTIES)) {
                    reader.beginObject();
                    while (reader.hasNext()) {
                        String subname = reader.nextName();
                        if(subname.equals(GeoConstants.GEOJSON_NAME)){
                            String val = reader.nextString();
                            isWGS = checkCRSSupportAndWGS(val, context);
                        }
                        else {
                            reader.skipValue();
                        }
                    }
                    reader.endObject();
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    reader.close();
    return isWGS;
}
 
源代码11 项目: android-vlc-remote   文件: JsonContentHandler.java
protected final Object parse(URLConnection connection) throws IOException {
    InputStream input;
    try {
        input = connection.getInputStream();
    } catch(FileNotFoundException ex) {
        throw new FileNotFoundException(FILE_NOT_FOUND);
    }
    JsonReader reader = new JsonReader(new InputStreamReader(input, "UTF-8"));
    try {
        return parse(reader);
    } finally {
        reader.close();
    }
}
 
源代码12 项目: United4   文件: United.java
/**
 * Plays the given song, by finding its R.raw id in the songs map, stored in properties
 * If notify is set set to true, will notify the webpage. For more information, see NotifierService's documentation
 *
 * @param song   the full name of the song to play
 * @param reload whether to notify the web page
 */
public static void playSong(String song, final boolean reload) {
    Log.i(TAG, "playSong called on " + song);
    // Find the filename in the map
    String songs = P.get("songs");
    JsonReader reader = new JsonReader(new StringReader(songs));
    String id = null;
    try {
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            String read = reader.nextString();
            //noinspection EqualsReplaceableByObjectsCall
            if (name.equals(song)) {
                id = read;
                Log.i(TAG, "Song id is " + id);
                break;
            }
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    // If we couldn't find it, fucking die
    if (id == null) {
        Log.e(TAG, "Song not found");
        throw new IllegalArgumentException("song not found");
    }
    Log.i(TAG, "Loading sound");
    // Stop any song in progress
    stop();
    // Set some properties in case the javascript forgot to set them
    P.set("is_playing", "true");
    P.set("current_song", song);
    if (player != null) {
        player.stop();
    }
    LoadControl loadControl = new DefaultLoadControl();
    player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
    player.addListener(new SongDoneListener());

    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getContext(), Util.getUserAgent(getContext(), "la/u/ncher"), bandwidthMeter);

    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

    MediaSource mediaSource = new ExtractorMediaSource(Uri.parse("https://niles.xyz/valhalla_music/" + id + ".mp3"), dataSourceFactory, extractorsFactory, null, null);
    player.setPlayWhenReady(true);
    player.prepare(mediaSource);
    if (reload) {
        NotifierService.notify(NotifierService.NotificationType.RELOAD_MUSIC);
    }
}
 
源代码13 项目: android-galaxyzoo   文件: LoginUtils.java
public static LoginResult parseLoginResponseContent(final InputStream content) throws IOException {
    //A failure by default.
    LoginResult result = new LoginResult(false, null, null);

    final InputStreamReader streamReader = new InputStreamReader(content, Utils.STRING_ENCODING);
    final JsonReader reader = new JsonReader(streamReader);
    reader.beginObject();
    boolean success = false;
    String apiKey = null;
    String userName = null;
    String message = null;
    while (reader.hasNext()) {
        final String name = reader.nextName();
        switch (name) {
            case "success":
                success = reader.nextBoolean();
                break;
            case "api_key":
                apiKey = reader.nextString();
                break;
            case "name":
                userName = reader.nextString();
                break;
            case "message":
                message = reader.nextString();
                break;
            default:
                reader.skipValue();
        }
    }

    if (success) {
        result = new LoginResult(true, userName, apiKey);
    } else {
        Log.info("Login failed.");
        Log.info("Login failure message: " + message);
    }

    reader.endObject();
    reader.close();

    streamReader.close();

    return result;
}
 
源代码14 项目: android_maplib   文件: GeoJSONUtil.java
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();
}
 
源代码15 项目: android_maplib   文件: GeoJSONUtil.java
public static void createLayerFromGeoJSONStream(VectorLayer layer, InputStream in, IProgressor progressor, boolean isWGS84) 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"));
    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();
}