最近做了一個阿里云物聯網遠程監控系統,需要從手機app端讀取阿里云物聯網的資料,訂閱的資料包Json格式為:
{"deviceName1":"device_0001","Temperature":29,"Humidity":45,"GeoLocation":{"CoordinateSystem":2,"Latitude":3*.**8914,"Longitude":11*.**6077}}
資料包包含設備編號、溫度、濕度,以及GPS地理坐標(坐標系統、經度和緯度),那么如何決議含有GPS地理坐標的Json格式資料呢?以下做一簡要介紹,
一、引入Gson決議包
import com.google.gson.Gson;
二、構造Json資料包類JsonRootBean
package com.linkkit.aiot_android_demo;
public class JsonRootBean {
public String deviceName1;
public int Temperature;
public int Humidity;
public GeoLocation GeoLocation;
}
三、構造地理坐標類GeoLocation
package com.linkkit.aiot_android_demo;
public class GeoLocation {
public int CoordinateSystem;
public double Latitude;
public double Longitude;
}
注意:JsonRootBean類中的設備名稱deviceName1、溫度Temperature、濕度Humidity、坐標GeoLocation,以及GeoLocation類中的坐標系統CoordinateSystem、經度Latitude和緯度Longitude,英文欄位名稱要與Json中欄位完全一致,
四、決議
TextView tv2 =(TextView)findViewById(R.id.textView2);
TextView tv3 =(TextView)findViewById(R.id.textView3);
TextView tv4 =(TextView)findViewById(R.id.textView4);
TextView tv5 =(TextView)findViewById(R.id.textView5);
TextView tv51 =(TextView)findViewById(R.id.textView51);
JsonRootBean newuser11=new Gson().fromJson(new String(message.getPayload()),JsonRootBean.class);
tv2.setText(newuser11.deviceName1);
tv3.setText(Integer.toString(newuser11.Temperature));
tv4.setText(Integer.toString(newuser11.Humidity));
tv5.setText(Double.toString(newuser11.GeoLocation.Latitude));
tv51.setText(Double.toString(newuser11.GeoLocation.Longitude));
其中,message.getPayload()為訂閱獲得的Json資料包,
五、決議結果

layout設計略
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/292580.html
標籤:其他
