Example usage for android.util JsonReader beginObject

List of usage examples for android.util JsonReader beginObject

Introduction

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

Prototype

public void beginObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.

Usage

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

@Nullable
private String getAgentName(@NotNull JsonReader reader) throws IOException {
    reader.beginObject();

    String result = null;//from w w  w .j  a v  a  2 s . c  om

    while (reader.hasNext()) {
        switch (reader.nextName()) {
        case "name":
            result = reader.nextString();
            break;
        default:
            reader.skipValue();
        }
    }

    reader.endObject();

    return result;
}

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

@Nullable
private String getHref(@NotNull JsonReader reader) throws IOException {
    reader.beginObject();

    String result = null;//from   w  w w .  j a  va2  s  .  com

    while (reader.hasNext()) {
        switch (reader.nextName()) {
        case "href":
            result = reader.nextString();
            break;
        default:
            reader.skipValue();
        }
    }

    reader.endObject();

    return result;
}

From source file:pedromendes.tempodeespera.HospitalDetailActivity.java

private List<Emergency> readHospitalDetailGetResponse(JsonReader reader) throws IOException {
    List<Emergency> result = null;
    reader.beginObject();
    while (reader.hasNext()) {
        result = readResult(reader);/*from  w w w .  j  a  v a2  s  .  c  om*/
    }
    reader.endObject();
    return result;
}

From source file:com.example.propertylist.handler.JsonPropertyHandler.java

/**
 * Loads the next observation into the property class.
 *
 * @param reader/*from  ww  w .j  a va 2  s .c  o  m*/
 *            the {@link android.util.JsonReader} containing the observation
 * @throws java.io.IOException
 */
private Property parseSalesData(JsonReader reader) throws IOException {
    Property property = new Property();
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("full_address") && reader.peek() != JsonToken.NULL) {
            property.setFullAddress(reader.nextString());
        } else if (name.equals("daft_url") && reader.peek() != JsonToken.NULL) {
            property.setDaftPropertyUrl(reader.nextString());
        } else if (name.equals("description") && reader.peek() != JsonToken.NULL) {
            property.setDescription(reader.nextString());
        } else if (name.equals("small_thumbnail_url") && reader.peek() != JsonToken.NULL) {
            property.setThumbnailUrl(reader.nextString());
        } else if (name.equals("medium_thumbnail_url") && reader.peek() != JsonToken.NULL) {
            property.setMediumThumbnailUrl(reader.nextString());
        } else if (name.equals("large_thumbnail_url") && reader.peek() != JsonToken.NULL) {
            property.setLargeThumbnailUrl(reader.nextString());
        } else {
            reader.skipValue();
        } // end if hasnext
    } // end while
    reader.endObject();
    return property;
}

From source file:com.morlunk.leeroy.LeeroyUpdateService.java

private void handleCheckUpdates(Intent intent, boolean notify, ResultReceiver receiver) {
    List<LeeroyApp> appList = LeeroyApp.getApps(getPackageManager());

    if (appList.size() == 0) {
        return;//from w w w  . j  a  va  2 s . c  o  m
    }

    List<LeeroyAppUpdate> updates = new LinkedList<>();
    List<LeeroyApp> notUpdatedApps = new LinkedList<>();
    List<LeeroyException> exceptions = new LinkedList<>();
    for (LeeroyApp app : appList) {
        try {
            String paramUrl = app.getJenkinsUrl() + "/api/json?tree=lastSuccessfulBuild[number,url]";
            URL url = new URL(paramUrl);
            URLConnection conn = url.openConnection();
            Reader reader = new InputStreamReader(conn.getInputStream());

            JsonReader jsonReader = new JsonReader(reader);
            jsonReader.beginObject();
            jsonReader.nextName();
            jsonReader.beginObject();

            int latestSuccessfulBuild = 0;
            String buildUrl = null;
            while (jsonReader.hasNext()) {
                String name = jsonReader.nextName();
                if ("number".equals(name)) {
                    latestSuccessfulBuild = jsonReader.nextInt();
                } else if ("url".equals(name)) {
                    buildUrl = jsonReader.nextString();
                } else {
                    throw new RuntimeException("Unknown key " + name);
                }
            }
            jsonReader.endObject();
            jsonReader.endObject();
            jsonReader.close();

            if (latestSuccessfulBuild > app.getJenkinsBuild()) {
                LeeroyAppUpdate update = new LeeroyAppUpdate();
                update.app = app;
                update.newBuild = latestSuccessfulBuild;
                update.newBuildUrl = buildUrl;
                updates.add(update);
            } else {
                notUpdatedApps.add(app);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            CharSequence appName = app.getApplicationInfo().loadLabel(getPackageManager());
            exceptions.add(new LeeroyException(app, getString(R.string.invalid_url, appName), e));
        } catch (IOException e) {
            e.printStackTrace();
            exceptions.add(new LeeroyException(app, e));
        }
    }

    if (notify) {
        NotificationManagerCompat nm = NotificationManagerCompat.from(this);
        if (updates.size() > 0) {
            NotificationCompat.Builder ncb = new NotificationCompat.Builder(this);
            ncb.setSmallIcon(R.drawable.ic_stat_update);
            ncb.setTicker(getString(R.string.updates_available));
            ncb.setContentTitle(getString(R.string.updates_available));
            ncb.setContentText(getString(R.string.num_updates, updates.size()));
            ncb.setPriority(NotificationCompat.PRIORITY_LOW);
            ncb.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            Intent appIntent = new Intent(this, AppListActivity.class);
            appIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            ncb.setContentIntent(
                    PendingIntent.getActivity(this, 0, appIntent, PendingIntent.FLAG_CANCEL_CURRENT));
            ncb.setAutoCancel(true);
            NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
            for (LeeroyAppUpdate update : updates) {
                CharSequence appName = update.app.getApplicationInfo().loadLabel(getPackageManager());
                style.addLine(getString(R.string.notify_app_update, appName, update.app.getJenkinsBuild(),
                        update.newBuild));
            }
            style.setSummaryText(getString(R.string.app_name));
            ncb.setStyle(style);
            ncb.setNumber(updates.size());
            nm.notify(NOTIFICATION_UPDATE, ncb.build());
        }

        if (exceptions.size() > 0) {
            NotificationCompat.Builder ncb = new NotificationCompat.Builder(this);
            ncb.setSmallIcon(R.drawable.ic_stat_error);
            ncb.setTicker(getString(R.string.error_checking_updates));
            ncb.setContentTitle(getString(R.string.error_checking_updates));
            ncb.setContentText(getString(R.string.click_to_retry));
            ncb.setPriority(NotificationCompat.PRIORITY_LOW);
            ncb.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            ncb.setContentIntent(PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
            ncb.setAutoCancel(true);
            ncb.setNumber(exceptions.size());
            nm.notify(NOTIFICATION_ERROR, ncb.build());
        }
    }

    if (receiver != null) {
        Bundle results = new Bundle();
        results.putParcelableArrayList(EXTRA_UPDATE_LIST, new ArrayList<>(updates));
        results.putParcelableArrayList(EXTRA_NO_UPDATE_LIST, new ArrayList<>(notUpdatedApps));
        results.putParcelableArrayList(EXTRA_EXCEPTION_LIST, new ArrayList<>(exceptions));
        receiver.send(0, results);
    }
}

From source file:com.thingsee.tracker.REST.KiiBucketRequestAsyncTask.java

private JSONObject readSingleData(JsonReader jsonReader) throws IOException, JSONException {
    JSONObject jsonObject = new JSONObject();
    jsonReader.beginObject();
    JsonToken token;/*from  w  ww  .jav a2  s . c  o  m*/
    do {
        String name = jsonReader.nextName();
        if ("sId".equals(name)) {
            jsonObject.put("sId", jsonReader.nextString());
        } else if ("val".equals(name)) {
            jsonObject.put("val", jsonReader.nextDouble());
        } else if ("ts".equals(name)) {
            jsonObject.put("ts", jsonReader.nextLong());
        } else if ("_owner".equals(name)) {
            jsonObject.put("_owner", jsonReader.nextString());
        }

        token = jsonReader.peek();
    } while (token != null && !token.equals(JsonToken.END_OBJECT));
    jsonReader.endObject();
    return jsonObject;
}

From source file:com.example.propertylist.handler.JsonPropertyHandler.java

/**
 * Populates properties data with the JSON data.
 *
 * @param reader// ww  w .j  a  va2 s . co  m
 *            the {@link android.util.JsonReader} used to read in the data
 * @return
 *         the propertyAds
 * @throws org.json.JSONException
 * @throws java.io.IOException
 */
public List<Property> populateAdsSales(JsonReader reader) throws JSONException, IOException {
    List<Property> propertyAds = new ArrayList<Property>();
    reader.beginObject();
    while (reader.hasNext()) {
        final String name = reader.nextName();
        final boolean isNull = reader.peek() == JsonToken.NULL;
        if (name.equals("ads") && !isNull) {
            propertyAds = parseDataArray(reader);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return propertyAds;
}

From source file:com.example.propertylist.handler.JsonPropertyHandler.java

/**
 * Populates property data with the JSON data.
 *
 * @param reader//from ww w . j  a  va  2 s.  c om
 *            the {@link android.util.JsonReader} used to read in the data
 * @return
 *              the propertyResults
 * @throws org.json.JSONException
 * @throws java.io.IOException
 */
public List<Property> populateResultsSales(JsonReader reader) throws JSONException, IOException {
    List<Property> propertyResults = new ArrayList<Property>();

    reader.beginObject();
    while (reader.hasNext()) {
        final String name = reader.nextName();
        final boolean isNull = reader.peek() == JsonToken.NULL;
        if (name.equals("results") && !isNull) {
            propertyResults = populateAdsSales(reader);
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return propertyResults;
}

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

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

    //noinspection TryFinallyCanBeTryWithResources
    try {//from   w  ww.  ja v a  2 s  .  c  o  m
        reader.beginObject();

        while (reader.hasNext()) {
            switch (reader.nextName()) {
            case "file":
                handleFiles(reader);
                break;
            default:
                reader.skipValue();
            }
        }

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

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

/**
 * Parse the next value as a {@link Map}. Children will be converted to a known type. In
 * general, this method does not handle {@link Set}s as children.
 *
 * @param reader The JsonReader to use. Calls to {@link JsonReader#beginObject()} and {@link
 * JsonReader#endObject()} will be taken care of by this method.
 * @param map The Map to populate.//from   w  w w . ja v  a  2  s . c  o m
 * @param valueClass The type of the Map value, corresponding to V in Map{@literal<}K,
 * V{@literal>}.
 * @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 <T> The value type of the Map, corresponding to V in Map{@literal<}K, V{@literal>}.
 */
public static <T> void parseAsMap(JsonReader reader, Map<String, T> map, Class<T> valueClass,
        JsonObjectParser<T> parser, String key) throws IOException {
    if (handleNull(reader)) {
        return;
    }

    final String discriminationName = ContextHolder.getContext().getSettings().getDiscriminationName();
    assertType(reader, key, JsonToken.BEGIN_OBJECT);
    reader.beginObject();
    while (reader.hasNext()) {
        T value;
        String name = reader.nextName();
        if (parser != null) {
            reader.beginObject();
            value = parser.parseJsonObject(null, reader, discriminationName, null);
            reader.endObject();
        } else {
            Object o = parseNextValue(reader, true);
            if (!valueClass.isInstance(o)) {
                throwMapException(name, key, valueClass, o);
            }
            value = cast(o);

        }
        map.put(name, value);
    }
    reader.endObject();
}