Example usage for android.util JsonReader nextName

List of usage examples for android.util JsonReader nextName

Introduction

In this page you can find the example usage for android.util JsonReader nextName.

Prototype

public String nextName() throws IOException 

Source Link

Document

Returns the next token, a JsonToken#NAME property name , and consumes it.

Usage

From source file:dk.cafeanalog.AnalogDownloader.java

public AnalogStatus isOpen() {
    HttpURLConnection connection = null;
    JsonReader reader = null;

    try {// w  ww. ja  v a  2  s  . c o m
        URL url = new URL("http", "cafeanalog.dk", "api/open");
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        reader = new JsonReader(new InputStreamReader(connection.getInputStream()));
        reader.beginObject();
        while (!reader.nextName().equals("open")) {
            reader.skipValue();
        }
        return reader.nextBoolean() ? AnalogStatus.OPEN : AnalogStatus.CLOSED;
    } catch (IOException e) {
        return AnalogStatus.UNKNOWN;
    } finally {
        if (connection != null)
            connection.disconnect();
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignored) {
            }
        }
    }
}

From source file:at.ac.tuwien.caa.docscan.logic.DataLog.java

private ShotLog readShotLog(JsonReader reader) throws IOException, ParseException {

    GPS gps = null;/*w w w .  j av a2  s.c om*/
    String dateString, fileName = null;
    Date date = null;
    boolean seriesMode = false;

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(FILE_NAME)) {
            fileName = reader.nextString();
        } else if (name.equals(DATE_NAME)) {
            dateString = reader.nextString();
            if (dateString != null)
                date = string2Date(dateString);
        } else if (name.equals(GPS_NAME)) {
            gps = readGPS(reader);
        } else if (name.equals(SERIES_MODE_NAME)) {
            seriesMode = reader.nextBoolean();
        }

    }
    reader.endObject();

    ShotLog shotLog = new ShotLog(fileName, gps, date, seriesMode);
    return shotLog;

}

From source file:com.tcity.android.ui.info.BuildInfoTask.java

private void handleResponse(@NotNull HttpResponse response) throws IOException, ParseException {
    JsonReader reader = new JsonReader(new InputStreamReader(response.getEntity().getContent()));

    //noinspection TryFinallyCanBeTryWithResources
    try {//from ww w .  j a va2  s. c o m
        reader.beginObject();

        BuildInfoData data = new BuildInfoData();
        SimpleDateFormat dateFormat = new SimpleDateFormat(Common.TEAMCITY_DATE_FORMAT);

        while (reader.hasNext()) {
            switch (reader.nextName()) {
            case "status":
                if (data.status == null) {
                    data.status = com.tcity.android.Status.valueOf(reader.nextString());
                }
                break;
            case "running":
                if (reader.nextBoolean()) {
                    data.status = com.tcity.android.Status.RUNNING;
                }
                break;
            case "branchName":
                data.branch = reader.nextString();
                break;
            case "defaultBranch":
                data.isBranchDefault = reader.nextBoolean();
                break;
            case "statusText":
                data.result = reader.nextString();
                break;
            case "waitReason":
                data.waitReason = reader.nextString();
                break;
            case "queuedDate":
                data.queued = dateFormat.parse(reader.nextString());
                break;
            case "startDate":
                data.started = dateFormat.parse(reader.nextString());
                break;
            case "finishDate":
                data.finished = dateFormat.parse(reader.nextString());
                break;
            case "agent":
                data.agent = getAgentName(reader);
                break;
            default:
                reader.skipValue();
            }
        }

        myResult = data;

        reader.endObject();
    } finally {
        reader.close();
    }
}

From source file:com.tcity.android.ui.info.BuildArtifactsTask.java

private void handleFiles(@NotNull JsonReader reader) throws IOException {
    reader.beginArray();//from   w w w.  ja  va 2 s. c  o  m

    List<BuildArtifact> result = new ArrayList<>();

    while (reader.hasNext()) {
        reader.beginObject();

        long size = -1;
        String name = null;
        String contentHref = null;
        String childrenHref = null;

        while (reader.hasNext()) {
            switch (reader.nextName()) {
            case "size":
                size = reader.nextLong();
                break;
            case "name":
                name = reader.nextString();
                break;
            case "children":
                childrenHref = getHref(reader);
                break;
            case "content":
                contentHref = getHref(reader);
                break;
            default:
                reader.skipValue();
            }
        }

        if (name == null) {
            throw new IllegalStateException("Invalid artifacts json: \"name\" is absent");
        }

        if (contentHref == null && childrenHref == null) {
            throw new IllegalStateException("Invalid artifacts json: \"content\" and \"children\" are absent");
        }

        result.add(new BuildArtifact(size, name, contentHref, childrenHref));

        reader.endObject();
    }

    reader.endArray();

    myResult = result;
}

From source file:com.workday.autoparse.json.parser.JsonParserUtils.java

/**
 * Parse the next value as an object. If the next value is {@link JsonToken#NULL}, returns
 * null.//from   w w w.  j a va  2s.c  o  m
 * <p/>
 * This method will use the provide parser, or if none is provided, will attempt find an
 * appropriate parser based on the discrimination value found in the next object. If none is
 * found, then this method returns a {@link JSONObject}.
 *
 * @param reader The JsonReader to use. Calls to {@link JsonReader#beginObject()} and {@link
 * JsonReader#endObject()} will be taken care of by this method.
 * @param parser The parser to use, or null if this method should find an appropriate one on its
 * own.
 * @param key The key corresponding to the current value. This is used to make more useful error
 * messages.
 * @param expectedType The expected class of the resulting object. If the result is not an
 * instance of this class, an exception is thrown.
 *
 * @throws IllegalStateException if the resulting object is not an instance of {@code
 * expectedType}.
 */
public static Object parseJsonObject(JsonReader reader, JsonObjectParser<?> parser, String key,
        Class<?> expectedType) throws IOException, IllegalStateException {
    if (handleNull(reader)) {
        return null;
    }
    assertType(reader, key, JsonToken.BEGIN_OBJECT);

    final String discriminationName = ContextHolder.getContext().getSettings().getDiscriminationName();
    String discriminationValue = null;
    Object result = null;
    reader.beginObject();
    if (parser != null) {
        result = parser.parseJsonObject(null, reader, discriminationName, null);
    } else if (reader.hasNext()) {
        String firstName = reader.nextName();
        final String discriminationKeyName = ContextHolder.getContext().getSettings().getDiscriminationName();
        if (discriminationKeyName.equals(firstName)) {
            discriminationValue = reader.nextString();
            parser = ContextHolder.getContext().getJsonObjectParserTable().get(discriminationValue);
            if (parser != null) {
                result = parser.parseJsonObject(null, reader, discriminationName, discriminationValue);
            } else {
                result = parseSpecificJsonObjectDelayed(reader, discriminationKeyName, discriminationValue);
            }
        } else {
            result = parseSpecificJsonObjectDelayed(reader, firstName, null);
        }

    }
    reader.endObject();

    if (result == null) {
        result = new JSONObject();
    }

    JsonObjectParser<?> unknownObjectParser = ContextHolder.getContext().getSettings().getUnknownObjectParser();
    if (result instanceof JSONObject && unknownObjectParser != null) {
        result = unknownObjectParser.parseJsonObject((JSONObject) result, null, discriminationName,
                discriminationValue);
    }

    if (expectedType != null && !(expectedType.isInstance(result))) {
        throw new IllegalStateException(
                String.format(Locale.US, "Could not convert value at \"%s\" to %s from %s.", key,
                        expectedType.getCanonicalName(), result.getClass().getCanonicalName()));
    }
    return result;
}

From source file:br.com.thinkti.android.filechooserfrag.fragFileChooserQuizlet.java

RowData parseSetJson(JsonReader reader) throws IOException {
    reader.beginObject();/*  w w  w . j av  a  2s .co  m*/
    RowData rowData = new RowData();

    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("title")) {
            rowData.name = reader.nextString();
        } else if (name.equals("description")) {
            rowData.description = reader.nextString();
            if (rowData.description.length() > 200)
                rowData.description = rowData.description.substring(0, 100);
        } else if (name.equals("id")) {
            rowData.id = reader.nextInt();
        } else if (name.equals("term_count")) {
            rowData.numCards = reader.nextInt();
        } else if (name.equals("modified_date")) {
            long value = reader.nextLong();
            rowData.lastModified = Data.SHORT_DATE_FORMAT.format(new Date(value * 1000));
            Log.d(Data.APP_ID, " modified_date   value=" + value + " formatted=" + rowData.lastModified
                    + " now=" + (new Date().getTime()));
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return rowData;
}

From source file:br.com.thinkti.android.filechooserfrag.fragFileChooserQuizlet.java

typVok parseSetDataJson(JsonReader reader) throws IOException {
    reader.beginObject();/* w w  w.  ja  v  a  2 s .c o  m*/
    typVok rowData = new typVok();

    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("term")) {
            rowData.Wort = reader.nextString();
        } else if (name.equals("id")) {
            long id = reader.nextLong();
            rowData.z = 0;
        } else if (name.equals("definition")) {
            rowData.Bed1 = reader.nextString();
            rowData.Bed2 = "";
            rowData.Bed3 = "";
        } else if (name.equals("image")) {
            try {
                reader.beginObject();
                while (reader.hasNext()) {
                    String strName = reader.nextName();
                    if (strName.equals("url")) {
                        String value = "<link://" + reader.nextString() + " "
                                + _main.getString(R.string.picture) + "/>";
                        rowData.Kom = value;
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
            } catch (Exception exception) {
                reader.skipValue();
                //String value = "<link://" + reader.nextString() + "/>";
                //rowData.Kom = value;
            }

        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    if (lib.libString.IsNullOrEmpty(rowData.Bed1)) {
        rowData.Bed1 = rowData.Kom;
        rowData.Bed2 = "";
        rowData.Bed3 = "";
    }
    return rowData;
}

From source file:br.com.thinkti.android.filechooserfrag.fragFileChooserQuizlet.java

private List<RowData> openPage() throws Exception {
    this.errorDescription = null;
    this.errorTitle = null;

    List<RowData> list = new ArrayList<RowData>();
    InputStream inputStream = null;
    try {/*w  w w . ja  v a 2 s. c  o m*/
        if (!blnPrivate) {
            URL url = new URL(getCatalogUrl());
            /*HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            if ( connection.getResponseCode() >= 400 ) {
               inputStream = connection.getErrorStream();
            }
            else {
               inputStream = connection.getInputStream();
            }*/
            inputStream = org.liberty.android.fantastischmemo.downloader.quizlet.lib.makeApiCall(url,
                    _main.QuizletAccessToken);
        } else {
            inputStream = org.liberty.android.fantastischmemo.downloader.quizlet.lib
                    .getUserPrivateCardsets(_main.QuizletUser, _main.QuizletAccessToken);
        }
        JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
        if (!blnPrivate) {
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();
                if ("total_pages".equals(name)) {
                    this.totalPages = reader.nextInt();
                    if (page > totalPages) {

                    }
                } else if ("total_results".equals(name)) {
                    this.totalResults = reader.nextInt();
                } else if ("page".equals(name)) {
                    this.page = reader.nextInt();
                } else if ("error_title".equals(name)) {
                    errorTitle = reader.nextString();
                } else if ("error_description".equals(name)) {
                    errorDescription = reader.nextString();
                } else if ("sets".equals(name)) {
                    getSets(reader, list);
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } else {
            getSets(reader, list);
        }

    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
    return list;
}

From source file:br.com.thinkti.android.filechooserfrag.fragFileChooserQuizlet.java

private List<typVok> openSet(String id) throws Exception {
    this.errorDescription = null;
    this.errorTitle = null;
    InputStream inputStream = null;
    List<typVok> list = new ArrayList<typVok>();
    String Kom = "";
    _main.vok.title = "";
    try {/* www .  ja  v a2s  .  c  o m*/
        URL url = new URL(getDeckUrl(id));
        /*
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        if ( connection.getResponseCode() >= 400 ) {
           inputStream = connection.getErrorStream();
        }
        else {
           inputStream = connection.getInputStream();
        }
        */
        inputStream = org.liberty.android.fantastischmemo.downloader.quizlet.lib.makeApiCall(url,
                _main.QuizletAccessToken);
        JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();
                if ("id".equals(name)) {
                    long intId = reader.nextLong();
                    /*if (page > totalPages) {
                            
                    }*/
                } else if ("url".equals(name)) {
                    String strUrl = reader.nextString();
                } else if ("title".equals(name)) {
                    String title = reader.nextString();
                    _main.vok.title = title;
                } else if ("created_by".equals(name)) {
                    String created_by = reader.nextString();
                    Kom = _main.getString(R.string.created_by) + " " + created_by + " "
                            + _main.getString((R.string.at)) + " <link://https://quizlet.com/ Quizlet/>";
                } else if ("term_count".equals(name)) {
                    int term_count = reader.nextInt();
                } else if ("lang_terms".equals(name)) {
                    String lang_terms = reader.nextString();
                    try {
                        _LangWord = new Locale(lang_terms.replace("-", "_"));
                    } catch (Throwable ex) {

                    }
                } else if ("lang_definitions".equals(name)) {
                    String lang_definitions = reader.nextString();
                    try {
                        _LangMeaning = (new Locale(lang_definitions.replace("-", "_")));
                    } catch (Throwable ex) {

                    }
                } else if ("terms".equals(name)) {
                    reader.beginArray();
                    while (reader.hasNext()) {
                        typVok v = parseSetDataJson(reader);
                        String kom = v.Kom;
                        v.Kom = Kom;
                        if (!lib.libString.IsNullOrEmpty(kom)) {
                            v.Kom += " " + kom;
                        }
                        list.add(v);
                    }
                    reader.endArray();
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        }
        reader.endArray();
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
    return list;
}

From source file:watch.oms.omswatch.parser.OMSConfigDBParser.java

private ContentValues readSingleRowData(JsonReader reader) {
    ContentValues contentValues = new ContentValues();
    String colName = null;//www.j  a  v a  2s  .  co m
    String colValue = null;
    try {
        reader.beginObject();

        while (reader.hasNext()) {
            colName = null;
            colValue = null;
            colName = reader.nextName();
            colValue = reader.nextString();
            if (colValue.equals(OMSConstants.NULL_STRING)) {
                colValue = OMSConstants.EMPTY_STRING;
            }

            if (!colName.equalsIgnoreCase("isdirty")) {
                contentValues.put(colName, colValue);
            }
        }
        reader.endObject();
    } catch (IOException e) {
        Log.e(TAG, "IOException:: ColName - " + (colName == null ? OMSConstants.EMPTY_STRING : colName));
        e.printStackTrace();
    }
    return contentValues;
}