use of org.json.JSONArray in project android-UniversalMusicPlayer by googlesamples.
the class RemoteJSONSource method iterator.
@Override
public Iterator<MediaMetadataCompat> iterator() {
try {
int slashPos = CATALOG_URL.lastIndexOf('/');
String path = CATALOG_URL.substring(0, slashPos + 1);
JSONObject jsonObj = fetchJSONFromUrl(CATALOG_URL);
ArrayList<MediaMetadataCompat> tracks = new ArrayList<>();
if (jsonObj != null) {
JSONArray jsonTracks = jsonObj.getJSONArray(JSON_MUSIC);
if (jsonTracks != null) {
for (int j = 0; j < jsonTracks.length(); j++) {
tracks.add(buildFromJSON(jsonTracks.getJSONObject(j), path));
}
}
}
return tracks.iterator();
} catch (JSONException e) {
LogHelper.e(TAG, e, "Could not retrieve music list");
throw new RuntimeException("Could not retrieve music list", e);
}
}
use of org.json.JSONArray in project android-maps-utils by googlemaps.
the class MyItemReader method read.
public List<MyItem> read(InputStream inputStream) throws JSONException {
List<MyItem> items = new ArrayList<MyItem>();
String json = new Scanner(inputStream).useDelimiter(REGEX_INPUT_BOUNDARY_BEGINNING).next();
JSONArray array = new JSONArray(json);
for (int i = 0; i < array.length(); i++) {
String title = null;
String snippet = null;
JSONObject object = array.getJSONObject(i);
double lat = object.getDouble("lat");
double lng = object.getDouble("lng");
if (!object.isNull("title")) {
title = object.getString("title");
}
if (!object.isNull("snippet")) {
snippet = object.getString("snippet");
}
items.add(new MyItem(lat, lng, title, snippet));
}
return items;
}
use of org.json.JSONArray in project gdk-compass-sample by googleglass.
the class Landmarks method populatePlaceList.
/**
* Populates the internal places list from places found in a JSON string. This string should
* contain a root object with a "landmarks" property that is an array of objects that represent
* places. A place has three properties: name, latitude, and longitude.
*/
private void populatePlaceList(String jsonString) {
try {
JSONObject json = new JSONObject(jsonString);
JSONArray array = json.optJSONArray("landmarks");
if (array != null) {
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.optJSONObject(i);
Place place = jsonObjectToPlace(object);
if (place != null) {
mPlaces.add(place);
}
}
}
} catch (JSONException e) {
Log.e(TAG, "Could not parse landmarks JSON string", e);
}
}
use of org.json.JSONArray in project graphhopper by graphhopper.
the class GraphHopperWeb method readErrors.
public static List<Throwable> readErrors(JSONObject json) {
List<Throwable> errors = new ArrayList<>();
JSONArray errorJson;
if (json.has("message")) {
if (json.has("hints")) {
errorJson = json.getJSONArray("hints");
} else {
// should not happen
errors.add(new RuntimeException(json.getString("message")));
return errors;
}
} else
return errors;
for (int i = 0; i < errorJson.length(); i++) {
JSONObject error = errorJson.getJSONObject(i);
String exClass = "";
if (error.has("details"))
exClass = error.getString("details");
String exMessage = error.getString("message");
if (exClass.equals(UnsupportedOperationException.class.getName()))
errors.add(new UnsupportedOperationException(exMessage));
else if (exClass.equals(IllegalStateException.class.getName()))
errors.add(new IllegalStateException(exMessage));
else if (exClass.equals(RuntimeException.class.getName()))
errors.add(new DetailedRuntimeException(exMessage, toMap(error)));
else if (exClass.equals(IllegalArgumentException.class.getName()))
errors.add(new DetailedIllegalArgumentException(exMessage, toMap(error)));
else if (exClass.equals(ConnectionNotFoundException.class.getName())) {
errors.add(new ConnectionNotFoundException(exMessage, toMap(error)));
} else if (exClass.equals(PointNotFoundException.class.getName())) {
int pointIndex = error.getInt("point_index");
errors.add(new PointNotFoundException(exMessage, pointIndex));
} else if (exClass.equals(PointOutOfBoundsException.class.getName())) {
int pointIndex = error.getInt("point_index");
errors.add(new PointOutOfBoundsException(exMessage, pointIndex));
} else if (exClass.isEmpty())
errors.add(new DetailedRuntimeException(exMessage, toMap(error)));
else
errors.add(new DetailedRuntimeException(exClass + " " + exMessage, toMap(error)));
}
if (json.has("message") && errors.isEmpty())
errors.add(new RuntimeException(json.getString("message")));
return errors;
}
use of org.json.JSONArray in project physical-web by google.
the class PhysicalWebCollection method jsonSerialize.
/**
* Create a JSON object that represents this data structure.
* @return a JSON serialization of this data structure.
*/
public JSONObject jsonSerialize() {
JSONObject jsonObject = new JSONObject();
// Serialize the UrlDevices
JSONArray urlDevices = new JSONArray();
for (UrlDevice urlDevice : mDeviceIdToUrlDeviceMap.values()) {
urlDevices.put(urlDevice.jsonSerialize());
}
jsonObject.put(DEVICES_KEY, urlDevices);
// Serialize the URL metadata
JSONArray metadata = new JSONArray();
for (PwsResult pwsResult : mBroadcastUrlToPwsResultMap.values()) {
metadata.put(pwsResult.jsonSerialize());
}
jsonObject.put(METADATA_KEY, metadata);
JSONObject iconMap = new JSONObject();
for (String iconUrl : mIconUrlToIconMap.keySet()) {
iconMap.put(iconUrl, new String(Base64.encodeBase64(getIcon(iconUrl)), Charset.forName("UTF-8")));
}
jsonObject.put(ICON_MAP_KEY, iconMap);
jsonObject.put(SCHEMA_VERSION_KEY, SCHEMA_VERSION);
return jsonObject;
}
Aggregations