use of org.json.JSONException 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.JSONException 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.JSONException in project physical-web by google.
the class JsonObjectRequest method readInputStream.
/**
* Helper method to read an HTTP response.
* @param is The InputStream.
* @return An object representing the HTTP response.
*/
protected JSONObject readInputStream(InputStream is) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
stringBuilder.append(line);
}
JSONObject jsonObject;
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException error) {
throw new IOException(error.toString());
}
return jsonObject;
}
use of org.json.JSONException in project physical-web by google.
the class PwsClient method resolve.
/**
* Send an HTTP request to the PWS to resolve a set of URLs.
* @param broadcastUrls The URLs to resolve.
* @param pwsResultCallback The callback to be run when the response is received.
*/
public void resolve(final Collection<String> broadcastUrls, final PwsResultCallback pwsResultCallback) {
// Create the response callback.
final long startTime = new Date().getTime();
JsonObjectRequest.RequestCallback requestCallback = new JsonObjectRequest.RequestCallback() {
private void recordResponse() {
pwsResultCallback.onResponseReceived(new Date().getTime() - startTime);
}
private PwsResult getPwsResult(JSONObject jsonUrlMetadata) {
switch(apiVersion) {
case 1:
return getV1PwsResult(jsonUrlMetadata);
case 2:
return getV2PwsResult(jsonUrlMetadata);
default:
throw new RuntimeException(UKNOWN_API_ERROR_MESSAGE);
}
}
private PwsResult getV1PwsResult(JSONObject jsonUrlMetadata) {
try {
return new PwsResult.Builder(jsonUrlMetadata.getString("id"), jsonUrlMetadata.getString("url")).setTitle(jsonUrlMetadata.optString("title")).setDescription(jsonUrlMetadata.optString("description")).setIconUrl(jsonUrlMetadata.optString("icon")).setGroupId(jsonUrlMetadata.optString("groupId")).build();
} catch (JSONException e) {
return null;
}
}
private PwsResult getV2PwsResult(JSONObject jsonUrlMetadata) {
try {
JSONObject jsonPageInfo = jsonUrlMetadata.getJSONObject("pageInfo");
return new PwsResult.Builder(jsonUrlMetadata.getString("scannedUrl"), jsonUrlMetadata.getString("resolvedUrl")).setTitle(jsonPageInfo.optString("title")).setDescription(jsonPageInfo.optString("description")).setIconUrl(jsonPageInfo.optString("icon")).build();
} catch (JSONException e) {
return null;
}
}
public void onResponse(JSONObject result) {
recordResponse();
// Build the metadata from the response.
JSONArray foundMetadata;
String jsonKey;
switch(apiVersion) {
case 1:
jsonKey = "metadata";
break;
case 2:
jsonKey = "results";
break;
default:
throw new RuntimeException(UKNOWN_API_ERROR_MESSAGE);
}
try {
foundMetadata = result.getJSONArray(jsonKey);
} catch (JSONException e) {
pwsResultCallback.onPwsResultError(broadcastUrls, 200, e);
return;
}
// Loop through the metadata for each url.
Set<String> foundUrls = new HashSet<>();
for (int i = 0; i < foundMetadata.length(); i++) {
JSONObject jsonUrlMetadata = foundMetadata.getJSONObject(i);
PwsResult pwsResult = getPwsResult(jsonUrlMetadata);
pwsResultCallback.onPwsResult(pwsResult);
foundUrls.add(pwsResult.getRequestUrl());
}
// See which urls the PWS didn't give us a response for.
Set<String> missed = new HashSet<>(broadcastUrls);
missed.removeAll(foundUrls);
for (String url : missed) {
pwsResultCallback.onPwsResultAbsent(url);
}
}
public void onError(int responseCode, Exception e) {
recordResponse();
pwsResultCallback.onPwsResultError(broadcastUrls, responseCode, e);
}
};
// Create the request.
String targetUrl = constructPwsResolveUrl();
JSONObject payload = new JSONObject();
try {
JSONArray urls = new JSONArray();
for (String url : broadcastUrls) {
JSONObject obj = new JSONObject();
obj.put("url", url);
urls.put(obj);
}
String jsonKey;
switch(apiVersion) {
case 1:
jsonKey = "objects";
break;
case 2:
jsonKey = "urls";
break;
default:
throw new RuntimeException(UKNOWN_API_ERROR_MESSAGE);
}
payload.put(jsonKey, urls);
} catch (JSONException e) {
pwsResultCallback.onPwsResultError(broadcastUrls, 0, e);
return;
}
Request request;
try {
request = new JsonObjectRequest(targetUrl, payload, requestCallback);
} catch (MalformedURLException e) {
pwsResultCallback.onPwsResultError(broadcastUrls, 0, e);
return;
}
makeRequest(request);
}
use of org.json.JSONException in project android-maps-utils by googlemaps.
the class GeoJsonParser method parseGeometry.
/**
* Parses a single GeoJSON geometry object containing a coordinates array or a geometries array
* if it has type GeometryCollection
*
* @param geoJsonGeometry geometry object to parse
* @return Geometry object
*/
private static Geometry parseGeometry(JSONObject geoJsonGeometry) {
try {
String geometryType = geoJsonGeometry.getString("type");
JSONArray geometryArray;
if (geometryType.equals(GEOMETRY_COLLECTION)) {
// GeometryCollection
geometryArray = geoJsonGeometry.getJSONArray(GEOMETRY_COLLECTION_ARRAY);
} else if (isGeometry(geometryType)) {
geometryArray = geoJsonGeometry.getJSONArray(GEOMETRY_COORDINATES_ARRAY);
} else {
// No geometries or coordinates array
return null;
}
return createGeometry(geometryType, geometryArray);
} catch (JSONException e) {
return null;
}
}
Aggregations