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

The following examples show how to use android.util.JsonReader#close() . 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: ScrapeTags.java    From NClientV2 with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 3
Source File: FirebaseInstallationServiceClient.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 4
Source File: LogResponse.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example 5
Source File: AboutJSONParser.java    From pivaa with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: LoadTags.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 7
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: JSONParser.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: MyViewModel.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
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();
  }
}
 
Example 10
Source File: GeoJSONUtil.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: JsonContentHandler.java    From android-vlc-remote with GNU General Public License v3.0 5 votes vote down vote up
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();
    }
}
 
Example 12
Source File: LoginUtils.java    From android-galaxyzoo with GNU General Public License v3.0 4 votes vote down vote up
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;
}
 
Example 13
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 14
Source File: GeoJSONUtil.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
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();
}