下面列出了android.util.JsonReader#nextString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Paring object
* @param reader
* @return
* @throws IOException
*/
private AboutRecord readMessage(JsonReader reader) throws IOException {
String name = "";
String description = "";
reader.beginObject();
while (reader.hasNext()) {
String key = reader.nextName();
if (key.equals("name")) {
name = reader.nextString();
} else if (key.equals("description")) {
description = reader.nextString();
} else {
reader.skipValue();
}
}
reader.endObject();
return new AboutRecord(name, description);
}
List<PlaceType> readPlaceTypesArray(JsonReader reader) throws IOException {
List<PlaceType> types = new ArrayList<>();
reader.beginArray();
while (reader.hasNext()) {
switch (reader.nextString()) {
case "route":
types.add(PlaceType.ROUTE);
break;
case "geocode":
types.add(PlaceType.GEOCODE);
break;
default:
reader.skipValue();
break;
}
}
reader.endArray();
return types;
}
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();
}
public Tag(JsonReader jr) throws IOException {
jr.beginObject();
while(jr.peek()!= JsonToken.END_OBJECT){
switch (jr.nextName()){
case "count":count=jr.nextInt();break;
case "type":type=TagType.typeByName(jr.nextString());break;
case "id":id=jr.nextInt();break;
case "name":name=jr.nextString();break;
case "url": LogUtility.d("Tag URL: "+jr.nextString());break;
default:jr.skipValue();break;
}
}
jr.endObject();
}
/**
* Reads in all the properties from the json file or some generic default properties if that failed
*/
private static void init() {
resetForAppStart();
if (getProperty("migrated").isEmpty()) {
try {
JsonReader reader = new JsonReader(new InputStreamReader(United.getContext().openFileInput(CONFIG)));
reader.beginObject();
int read = 0;
while (reader.hasNext()) {
read++;
String key = reader.nextName();
String value = reader.nextString();
Log.d(TAG, "MIGRATION: Read prop " + key);
setProperty(key, value);
}
resetForAppStart();
if (read == 0) {
setFirstRunProperties();
}
setProperty("migrated", "success");
} catch (Exception e) {
e.printStackTrace();
setFirstRunProperties();
setProperty("migrated", "errors");
}
} else if (getProperty("migrated").equalsIgnoreCase("reset")) {
setFirstRunProperties();
setProperty("migrated", "reset_complete");
}
}
List<PlacePhoto> readPhotosArray(JsonReader reader) throws IOException {
List<PlacePhoto> photos = new ArrayList<>();
reader.beginArray();
while (reader.hasNext()) {
int height = -1;
int width = -1;
String photoReference = null;
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "height":
height = reader.nextInt();
break;
case "width":
width = reader.nextInt();
break;
case "photo_reference":
photoReference = reader.nextString();
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
photos.add(new PlacePhoto(height, width, photoReference));
}
reader.endArray();
return photos;
}
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;
}
private static ZCashTransactionInput readTxSingleInput(JsonReader reader) throws IOException {
ZCashTransactionInput input = new ZCashTransactionInput();
reader.beginObject();
while (reader.peek() != JsonToken.END_OBJECT) {
String name = reader.nextName();
switch (name) {
case COINBASE:
input.coinbase = reader.nextString();
break;
case SEQUENCE:
input.sequence = reader.nextLong();
break;
case TXID:
input.txid = reader.nextString();
break;
case VOUT:
input.n = reader.nextLong();
break;
case SCRIPTSIG:
reader.skipValue();
break;
case RETRIEVEDVOUT:
input.copyDataFrom(readTxSingleOutput(reader));
break;
default:
reader.skipValue();
}
}
reader.endObject();
return input;
}
List<AddressComponent> readAddressComponentsArray(JsonReader reader) throws IOException {
List<AddressComponent> addressComponents = new ArrayList<>();
reader.beginArray();
while (reader.hasNext()) {
String longName = null;
String shortName = null;
List<AddressComponentType> types = null;
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "long_name":
longName = reader.nextString();
break;
case "short_name":
shortName = reader.nextString();
break;
case "types":
types = readAddressComponentTypesArray(reader);
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
addressComponents.add(new AddressComponent(longName, shortName, types));
}
reader.endArray();
return addressComponents;
}
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();
}
@Override
public void onMessage(WebSocket webSocket, String text) {
Integer replyID = null;
try {
JsonReader reader = new JsonReader(new StringReader(text));
String result = null;
reader.beginObject();
while (reader.hasNext()) {
String field = reader.nextName();
if (JsonToken.NULL == reader.peek()) {
reader.skipValue();
continue;
}
if ("replyID".equals(field)) {
replyID = reader.nextInt();
} else if ("result".equals(field)) {
result = reader.nextString();
} else if ("error".equals(field)) {
String error = reader.nextString();
abort(error, new JavascriptException(error));
}
}
if (replyID != null) {
triggerRequestSuccess(replyID, result);
}
} catch (IOException e) {
if (replyID != null) {
triggerRequestFailure(replyID, e);
} else {
abort("Parsing response message from websocket failed", e);
}
}
}
public Earthquake readEarthquake(JsonReader reader) throws IOException {
String id = null;
Location location = null;
Earthquake earthquakeProperties = null;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("id")) {
// The ID is stored as a value.
id = reader.nextString();
} else if (name.equals("geometry")) {
// The location is stored as a geometry object
// that must be parsed.
location = readLocation(reader);
} else if (name.equals("properties")) {
// Most of the earthquake details are stored as a
// properties object that must be parsed.
earthquakeProperties = readEarthquakeProperties(reader);
} else {
reader.skipValue();
}
}
reader.endObject();
// Construct a new Earthquake based on the parsed details.
return new Earthquake(id,
earthquakeProperties.getDate(),
earthquakeProperties.getDetails(),
location,
earthquakeProperties.getMagnitude(),
earthquakeProperties.getLink());
}
@NonNull
private static StickerPack readStickerPack(@NonNull JsonReader reader) throws IOException, IllegalStateException {
reader.beginObject();
String identifier = null;
String name = null;
String publisher = null;
String trayImageFile = null;
String publisherEmail = null;
String publisherWebsite = null;
String privacyPolicyWebsite = null;
String licenseAgreementWebsite = null;
String imageDataVersion = "";
boolean avoidCache = false;
List<Sticker> stickerList = null;
while (reader.hasNext()) {
String key = reader.nextName();
switch (key) {
case "identifier":
identifier = reader.nextString();
break;
case "name":
name = reader.nextString();
break;
case "publisher":
publisher = reader.nextString();
break;
case "tray_image_file":
trayImageFile = reader.nextString();
break;
case "publisher_email":
publisherEmail = reader.nextString();
break;
case "publisher_website":
publisherWebsite = reader.nextString();
break;
case "privacy_policy_website":
privacyPolicyWebsite = reader.nextString();
break;
case "license_agreement_website":
licenseAgreementWebsite = reader.nextString();
break;
case "stickers":
stickerList = readStickers(reader);
break;
case "image_data_version":
imageDataVersion = reader.nextString();
break;
case "avoid_cache":
avoidCache = reader.nextBoolean();
break;
default:
reader.skipValue();
}
}
if (TextUtils.isEmpty(identifier)) {
throw new IllegalStateException("identifier cannot be empty");
}
if (TextUtils.isEmpty(name)) {
throw new IllegalStateException("name cannot be empty");
}
if (TextUtils.isEmpty(publisher)) {
throw new IllegalStateException("publisher cannot be empty");
}
if (TextUtils.isEmpty(trayImageFile)) {
throw new IllegalStateException("tray_image_file cannot be empty");
}
if (stickerList == null || stickerList.size() == 0) {
throw new IllegalStateException("sticker list is empty");
}
if (identifier.contains("..") || identifier.contains("/")) {
throw new IllegalStateException("identifier should not contain .. or / to prevent directory traversal");
}
if (TextUtils.isEmpty(imageDataVersion)) {
throw new IllegalStateException("image_data_version should not be empty");
}
reader.endObject();
final StickerPack stickerPack = new StickerPack(identifier, name, publisher, trayImageFile, publisherEmail, publisherWebsite, privacyPolicyWebsite, licenseAgreementWebsite, imageDataVersion, avoidCache);
stickerPack.setStickers(stickerList);
return stickerPack;
}
private Sample readOfflineEntry(JsonReader reader) throws IOException {
String sampleName = null;
String downloadUri = null;
String offlineUri = null;
String extension = null;
UUID drmUuid = null;
String drmLicenseUrl = null;
String[] drmKeyRequestProperties = null;
boolean preferExtensionDecoders = false;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
switch (name) {
case "name":
sampleName = reader.nextString();
break;
case "download_uri":
downloadUri = reader.nextString();
break;
case "offline_uri":
offlineUri = reader.nextString();
break;
case "extension":
extension = reader.nextString();
break;
case "drm_scheme":
drmUuid = getDrmUuid(reader.nextString());
break;
case "drm_license_url":
drmLicenseUrl = reader.nextString();
break;
case "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":
preferExtensionDecoders = reader.nextBoolean();
break;
default:
throw new ParserException("Unsupported attribute name: " + name);
}
}
reader.endObject();
return new OfflineSample(sampleName,drmUuid,drmLicenseUrl,drmKeyRequestProperties,
preferExtensionDecoders,offlineUri,downloadUri);
}
private static ZCashTransactionDetails_taddr readTx(JsonReader reader) throws IOException, IllegalStateException {
ZCashTransactionDetails_taddr tx = new ZCashTransactionDetails_taddr();
if(reader.peek() != JsonToken.BEGIN_OBJECT) {
throw new IOException("Cannot parse JSON");
}
reader.beginObject();
while (reader.peek() != JsonToken.END_OBJECT) {
String name = reader.nextName();
switch (name) {
case HASH:
tx.hash = reader.nextString();
break;
case MAINCHAIN:
tx.mainChain = reader.nextBoolean();
break;
case FEE:
tx.fee = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case TYPE:
tx.type = reader.nextString();
break;
case SHIELDED:
tx.shielded = reader.nextBoolean();
break;
case INDEX:
tx.index = reader.nextLong();
break;
case BLOCKHASH:
tx.blockHash = reader.nextString();
break;
case BLOCKHEIGHT:
tx.blockHeight = reader.nextLong();
break;
case VERSION:
tx.version = reader.nextLong();
break;
case LOCKTIME:
tx.locktime = reader.nextLong();
break;
case TIME:
tx.time = reader.nextLong();
break;
case TIMESTAMP:
tx.timestamp = reader.nextLong();
break;
case VIN:
tx.vin = readTxInputs(reader);
break;
case VOUT:
tx.vout = readTxOutputs(reader, null);
break;
case VJOINSPLIT:
skipJoinSplits(reader);
break;
case VALUE:
tx.value = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case OUTPUTVALUE:
tx.outputValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case SHIELDEDVALUE:
tx.shieldedValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
default:
reader.skipValue();
}
}
reader.endObject();
return tx;
}
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);
}
}
private static String readFieldString(JsonReader reader) throws IOException {
reader.nextName();
return reader.nextString();
}
private static ZCashTransactionDetails_taddr readTx(JsonReader reader) throws IOException, IllegalStateException {
ZCashTransactionDetails_taddr tx = new ZCashTransactionDetails_taddr();
if(reader.peek() != JsonToken.BEGIN_OBJECT) {
throw new IOException("Cannot parse JSON");
}
reader.beginObject();
while (reader.peek() != JsonToken.END_OBJECT) {
String name = reader.nextName();
switch (name) {
case HASH:
tx.hash = reader.nextString();
break;
case MAINCHAIN:
tx.mainChain = reader.nextBoolean();
break;
case FEE:
tx.fee = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case TYPE:
tx.type = reader.nextString();
break;
case SHIELDED:
tx.shielded = reader.nextBoolean();
break;
case INDEX:
tx.index = reader.nextLong();
break;
case BLOCKHASH:
tx.blockHash = reader.nextString();
break;
case BLOCKHEIGHT:
tx.blockHeight = reader.nextLong();
break;
case VERSION:
tx.version = reader.nextLong();
break;
case LOCKTIME:
tx.locktime = reader.nextLong();
break;
case TIME:
tx.time = reader.nextLong();
break;
case TIMESTAMP:
tx.timestamp = reader.nextLong();
break;
case VIN:
tx.vin = readTxInputs(reader);
break;
case VOUT:
tx.vout = readTxOutputs(reader, null);
break;
case VJOINSPLIT:
skipJoinSplits(reader);
break;
case VALUE:
tx.value = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case OUTPUTVALUE:
tx.outputValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
case SHIELDEDVALUE:
tx.shieldedValue = Double.valueOf(reader.nextDouble() * 1e8).longValue();
break;
default:
reader.skipValue();
}
}
reader.endObject();
return tx;
}
@Override
public void run() {
try {
HttpURLConnection request = (HttpURLConnection) new URL("https://api.github.com/repos/TheAndroidMaster/MediaNotification/contributors").openConnection();
request.connect();
JsonReader reader = new JsonReader(new InputStreamReader((InputStream) request.getContent()));
reader.setLenient(true);
reader.beginArray();
reader.skipValue();
while (reader.hasNext()) {
reader.beginObject();
String name = null, imageUrl = null, url = null;
while (reader.hasNext()) {
switch (reader.nextName()) {
case "login":
name = reader.nextString();
break;
case "avatar_url":
imageUrl = reader.nextString();
break;
case "html_url":
url = reader.nextString();
break;
default:
reader.skipValue();
}
}
contributors.add(new ContributorData(name, imageUrl, url));
reader.endObject();
}
reader.endArray();
} catch (Exception ignored) {
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
AboutDialog dialog = dialogReference.get();
if (dialog != null) {
dialog.contributorView.getAdapter().notifyDataSetChanged();
for (final ContributorData contributor : contributors) {
new ContributorThread(dialog, contributor).start();
}
}
}
});
}
List<PlaceReview> readReviewsArray(JsonReader reader) throws IOException {
List<PlaceReview> reviews = new ArrayList<>();
reader.beginArray();
while (reader.hasNext()) {
List<RatingAspect> aspects = null;
String authorName = null;
String authorUrl = null;
String language = null;
int rating = -1;
String text = null;
long time = -1L;
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "aspects":
aspects = readAspectsArray(reader);
break;
case "author_name":
authorName = reader.nextString();
break;
case "author_url":
authorUrl = reader.nextString();
break;
case "language":
language = reader.nextString();
break;
case "rating":
rating = reader.nextInt();
break;
case "text":
text = reader.nextString();
break;
case "time":
time = reader.nextLong();
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
reviews.add(new PlaceReview(aspects, authorName, authorUrl, language, rating, text, time));
}
reader.endArray();
return reviews;
}