-
Notifications
You must be signed in to change notification settings - Fork 982
/
ImageryMetaData.java
58 lines (46 loc) · 2.26 KB
/
ImageryMetaData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.osmdroid.tileprovider.tilesource.bing;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* ImageryMetaData storage. Class used to decode valid ImageryMetaData.
*/
public class ImageryMetaData {
// Useful fields found in ImageryMetaData response
private final static String STATUS_CODE = "statusCode";
private final static String AUTH_RESULT_CODE = "authenticationResultCode";
private final static String AUTH_RESULT_CODE_VALID = "ValidCredentials";
private final static String RESOURCE_SETS = "resourceSets";
private final static String ESTIMATED_TOTAL = "estimatedTotal";
private final static String RESOURCE = "resources";
/**
* Parse a JSON string containing ImageryMetaData response
*
* @param a_jsonContent the JSON content string
* @throws Exception
* @return ImageryMetaDataResource object containing parsed information
*/
static public ImageryMetaDataResource getInstanceFromJSON(final String a_jsonContent) throws Exception {
if (a_jsonContent == null) {
throw new Exception("JSON to parse is null");
}
/// response code should be 200 and authorization should be valid (valid BingMap key)
final JSONObject jsonResult = new JSONObject(a_jsonContent);
final int statusCode = jsonResult.getInt(STATUS_CODE);
if (statusCode != 200) {
throw new Exception("Status code = " + statusCode);
}
if (AUTH_RESULT_CODE_VALID.compareToIgnoreCase(jsonResult.getString(AUTH_RESULT_CODE)) != 0) {
throw new Exception("authentication result code = " + jsonResult.getString(AUTH_RESULT_CODE));
}
// get first valid resource information
final JSONArray resultsSet = jsonResult.getJSONArray(RESOURCE_SETS);
if (resultsSet == null || resultsSet.length() < 1) {
throw new Exception("No results set found in json response");
}
if (resultsSet.getJSONObject(0).getInt(ESTIMATED_TOTAL) <= 0) {
throw new Exception("No resource found in json response");
}
final JSONObject resource = resultsSet.getJSONObject(0).getJSONArray(RESOURCE).getJSONObject(0);
return ImageryMetaDataResource.getInstanceFromJSON(resource, jsonResult);
}
}