public class GetJson {
private int WKID; // 坐标系
private int DPI;// 图片分辨率
private Point originPoint;// 圆点坐标
private int rows;// 瓦片高度
private int cols;// 瓦片宽度
private double[] RESOLUTION;// 分辨率数组
private double[] SCALE;// 比列尺数组
private Envelope INIEIALEXTENT;// 初始化显示范围
private Envelope FULLEXTENT;// 最大显示范围
private String jsonUrl;
public int getWKID() {
return WKID;
}
public int getDPI() {
return DPI;
}
public Point getOriginPoint() {
return originPoint;
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public double[] getRESOLUTION() {
return RESOLUTION;
}
public double[] getSCALE() {
return SCALE;
}
public Envelope getINIEIALEXTENT() {
return INIEIALEXTENT;
}
public Envelope getFULLEXTENT() {
return FULLEXTENT;
}
public GetJson(String url) {
// TODO Auto-generated constructor stub
this.jsonUrl = url;
getJsonStr(url);
}
public void getJsonStr(String url) {
HttpGet httpGet = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();
StringBuilder builder = new StringBuilder();
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(httpEntity.getContent()));
String str;
for (str = bufferedReader.readLine(); str != null; str = bufferedReader
.readLine()) {
builder.append(str);
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// 获取瓦片信息
JSONObject jsonObject = new JSONObject(builder.toString())
.getJSONObject("tileInfo");
rows = jsonObject.getInt("rows");
cols = jsonObject.getInt("cols");
DPI = jsonObject.getInt("dpi");
// 获取圆点坐标
JSONObject jsonObject1 = new JSONObject(builder.toString())
.getJSONObject("tileInfo").getJSONObject("origin");
Double x = jsonObject1.getDouble("x");
Double y = jsonObject1.getDouble("y");
originPoint = new Point(x, y);
// 获取显示范围
JSONObject jsonObject2 = new JSONObject(builder.toString())
.getJSONObject("initialExtent");
Double xmin = jsonObject2.getDouble("xmin");
Double ymin = jsonObject2.getDouble("ymin");
Double xmax = jsonObject2.getDouble("xmax");
Double ymax = jsonObject2.getDouble("ymax");
INIEIALEXTENT = new Envelope(xmin, ymin, xmax, ymax);
JSONObject jsonObject3 = new JSONObject(builder.toString())
.getJSONObject("fullExtent");
Double Xmin = jsonObject3.getDouble("xmin");
Double Ymin = jsonObject3.getDouble("ymin");
Double Xmax = jsonObject3.getDouble("xmax");
Double Ymax = jsonObject3.getDouble("ymax");
FULLEXTENT = new Envelope(Xmin, Ymin, Xmax, Ymax);
// 获取坐标系
JSONObject jsonObject4 = new JSONObject(builder.toString())
.getJSONObject("spatialReference");
WKID = jsonObject4.getInt("wkid");
// 获取分辨率和比例尺数组
JSONArray jsonArray = jsonObject.getJSONArray("lods");
int k = jsonArray.length();
RESOLUTION = new double[k];
SCALE = new double[k];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jObject = (JSONObject)jsonArray.opt(i);
RESOLUTION[i] = jObject.getDouble("resolution");
SCALE[i] = jObject.getDouble("scale");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
String msg = e.toString();
Log.i("解析JSON数据异常:", msg);
}
}
}
private int WKID; // 坐标系
private int DPI;// 图片分辨率
private Point originPoint;// 圆点坐标
private int rows;// 瓦片高度
private int cols;// 瓦片宽度
private double[] RESOLUTION;// 分辨率数组
private double[] SCALE;// 比列尺数组
private Envelope INIEIALEXTENT;// 初始化显示范围
private Envelope FULLEXTENT;// 最大显示范围
private String jsonUrl;
public int getWKID() {
return WKID;
}
public int getDPI() {
return DPI;
}
public Point getOriginPoint() {
return originPoint;
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public double[] getRESOLUTION() {
return RESOLUTION;
}
public double[] getSCALE() {
return SCALE;
}
public Envelope getINIEIALEXTENT() {
return INIEIALEXTENT;
}
public Envelope getFULLEXTENT() {
return FULLEXTENT;
}
public GetJson(String url) {
// TODO Auto-generated constructor stub
this.jsonUrl = url;
getJsonStr(url);
}
public void getJsonStr(String url) {
HttpGet httpGet = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();
StringBuilder builder = new StringBuilder();
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(httpEntity.getContent()));
String str;
for (str = bufferedReader.readLine(); str != null; str = bufferedReader
.readLine()) {
builder.append(str);
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// 获取瓦片信息
JSONObject jsonObject = new JSONObject(builder.toString())
.getJSONObject("tileInfo");
rows = jsonObject.getInt("rows");
cols = jsonObject.getInt("cols");
DPI = jsonObject.getInt("dpi");
// 获取圆点坐标
JSONObject jsonObject1 = new JSONObject(builder.toString())
.getJSONObject("tileInfo").getJSONObject("origin");
Double x = jsonObject1.getDouble("x");
Double y = jsonObject1.getDouble("y");
originPoint = new Point(x, y);
// 获取显示范围
JSONObject jsonObject2 = new JSONObject(builder.toString())
.getJSONObject("initialExtent");
Double xmin = jsonObject2.getDouble("xmin");
Double ymin = jsonObject2.getDouble("ymin");
Double xmax = jsonObject2.getDouble("xmax");
Double ymax = jsonObject2.getDouble("ymax");
INIEIALEXTENT = new Envelope(xmin, ymin, xmax, ymax);
JSONObject jsonObject3 = new JSONObject(builder.toString())
.getJSONObject("fullExtent");
Double Xmin = jsonObject3.getDouble("xmin");
Double Ymin = jsonObject3.getDouble("ymin");
Double Xmax = jsonObject3.getDouble("xmax");
Double Ymax = jsonObject3.getDouble("ymax");
FULLEXTENT = new Envelope(Xmin, Ymin, Xmax, Ymax);
// 获取坐标系
JSONObject jsonObject4 = new JSONObject(builder.toString())
.getJSONObject("spatialReference");
WKID = jsonObject4.getInt("wkid");
// 获取分辨率和比例尺数组
JSONArray jsonArray = jsonObject.getJSONArray("lods");
int k = jsonArray.length();
RESOLUTION = new double[k];
SCALE = new double[k];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jObject = (JSONObject)jsonArray.opt(i);
RESOLUTION[i] = jObject.getDouble("resolution");
SCALE[i] = jObject.getDouble("scale");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
String msg = e.toString();
Log.i("解析JSON数据异常:", msg);
}
}
}