-
1 学习目的
-
2 引导训练
学习目的
学习Java以及Tomcat服务器的部署
Android端服务器数据的获取
感知数据的数据抽象以及解析方法
通过本模块的独立学习与实验,掌握系统后台部署的技术实训
手机端JSON数据
通过存储在智慧校园应用数据服务器中的数据的调取及读取,完成智慧校园环境感知推送web服务器的搭建,并在应用端完成数据的获取及解析处理。其所涉及到的关键技术的操作包括:
智慧校园环境感知系统推送Web服务器的搭建;
Android端智慧校园感知系统环境服务器数据的获得;
Android端智慧校园感知数据的抽象解析方法。
案例1:智慧校园数据http数据获取。
package com. ict. campusperception. tools;
import org. apache. http. HttpResponse;
import org. apache. http. HttpStatus;
import org. apache. http. client. HttpClient;
import org. apache. http. client. methods. HttpGet;
import org. apache. http. impl. client. DefaultHttpClient,
import org. apache. http. params. BasicHttpParams;
import org. apache. http. params. HttpConnectionParams;
import org. apache. http. params. HttpParams;
import org. apache. http. util. EntityUtils;
import android. os. StrictMode;
//获取Http数据的class
public class GetService{
public static String getHttpJSON(String url){
StrictMode. setThreadPolicy( new StrictMode. ThreadPolicy. Builder( )
.detectDiskReads ( )
.detectDiskWrites ( )
.detectNetwork ( )
.PenajtyLog( )
.build());
//配置服务器URL
HttpGet httpRequest=new HttpGet(url);
try{
//配置基本配置参数
HttpParams params=new BasicHttpParams();
HttpConnectionParams. setConnectionTimeout(Params,5*1000);
HttpConnectionParams. setSoTimeout(params,8*1000);
HttpConnectionParams. setSocketBufferSize(params, 8192);
//获取服务器的返回值
HttpClient httpclient=new DefaultHttpClient(params);
HttpResponse httpResponse=httpclient. execute(httpRequest);
if (httpResponse. getStatusLine(). getStatusCode() = = HttpStatus. SC_OK) {
String jsonStr = EntityUtils. toString( httpResponse. getEntity( ),
"UTF - 8" ) ;
return jsonStr;
}
}catch (Exception e) {
e. printStackTrace( ) ;
System. out. println( "error on getHttpJSON : " + e. getMessage());
}
return null;
}
}
完成对http获取数据的解析。通过DAO数据抽象的概念进行数据封装,并完成数据解析。
案例2:智慧校园感知数据的封装解析。
package com.ict.campusperception.tools;
import java.util.Arraylist;
import org.json.JSONArray;//json数据类
import org.json.JSONException;
import org.json.JSONObject;//json解析类
//数据抽象类
import com. ict. campusperception. databean. AirDataForjson;
import com. ict. campusperception. databean. AirListjson;
import com. ict. campusperception. databean. AirMes;
import com. ict. campusperception. databean. C02DataForJson;
import com. ict. campusperception. databean. C02ListJson;
import com. ict. campusperception. databean. C02Mes;
import com. ict. campusperception. databean. LatestAirJson;
import com. ict. campusperception. databean. LatestC02Json;
import com. ict. campusperception. databean. NodeInfoForJson;
import com. ict. campusperception. databean. NodeListJson;
public class JsonService {
private static String json_to_parse ="";
System out. println( http +"1atestairmes?'id=" +id+"");
json_to_parse = GetService.getHttpJSON( http + "latesgtairmes?id="+id+"");
if ( json_to_pase = = null)
{
System.out. println( "json meiyou huoqu" ) ;
return null;
}
try {
JSONObject j_root= new JSONObject(json_to_parse);
JSONObject j_msg = (JSONObject)j_root. get("airmes");
JSONObject j_node = (JSONObject)j_root. get("node");
msg.setMoteid_ID( j_msg.getlnt( " moteid_ID" ));
//
msg.setTimestampArrive_TM( Timestamp. valueOf ( j_msg.getString("timestampArrive_TM)));
msg. setTemperature_ID(j_msg.getlnt( "temperature_ID"));
msg. setHumidity_ID( j_msg. getlnt( "humidity_ID"));
msg. setLight_ID( j_msg. getlnt ( "1ight_ID" ));
msg. setADC_Valtage_ID(j_msg.getlnt( "ADC_Valtage_ID" )) ;
int nodeID = j_node. getlnt( "moteid_ID" );
node. setMoteid_ID(nodeID) ;
node. setMoteid_Type((nodeID % 1000)>800?2 : 1) ;
node. setLatitude ( j_node.getDouble( " latitude" ));
node. setLongitude ( j_node.getDouble( " longitude"));
}catch (JSONExcception e) {
System. out. println("json jiexi shibai");
e.printStackTrace();
return null;
}
//为数据传输对象填充成员对象
msg_json.setLatest_ msg_json(msg);
msg_json.setLatest_ node_json(node);
//返回数据传输对象
return msg_json;
}
public static LatestCO2Json getData(LatestCO2Json msg_json,String http,String id){
//为数据传输对象初始化成员对象
C02DataForJson msg=new C02DataForJson();
NodeInfoForJson node=new NodeInfoForJson();
//解析json并填人对应成员对象
json_to_parse=GetService.getHttpJSON( http+"latestco2mes? id="+id"");
if(json_to_parse==null)
return null;
try{
JSONObject j_root=new JSONObject(json_to_parse);
JSONObject j_msg=(JSONObject) j_root.get("co2mes");
JSONObject j_node=(JSONObject) j_root.get("node");
msg.setMote_ID(j_msg.getInt("moteid_ID));
//
msg.setTimestampArrive_TM(Timestamp.valueOf ( j_msg.getString("timestampArrive_TM")));
msg.setC02 _ID( j_msg.getlnt("C02_ID"));
msg.setADC_Vcaltage_ID( j_msg. getlnt("ADC_Valtage_ID"));
int nodeID = j_node.getlnt("moteid_ID");
node.setMoteid_ID(nodeID) ;
node.setMoteid_Type((nodeID % 1000)>800?2:1);
node.setLatitude(j_node.getDouble("latitude"));
node.setLongitude(j_node.getDouble("longitude"));
} catch (JSONException e) {
e.printStackTrace( );
return null;
}
//为数据传输对象填充成员对象
msg_json.setLatest_msg_json( msg);
msg_json.setLatest_node_json( node);
//返回数据传输对象
return msg_json;
}
//type:dayavgairmes 过去24小时 weekavgairmes 过去7天 montbavgairmes过去30天yearavgairmes过去12个月
public static AirListJson getData(AirListJson msg_list_json, String http,String id, long datetime,
String type){
//为数据传输对象初始化成员对象
ArrayList<AirMes> msg_list=new ArrayList<AirMes>();
msg_list_json=new AirListjson();
//解析json并填人对应成员对象
System. out. println( "cccccccccccccccccccc1221"+ http+""+type+"?id="+id+"&datetime="+ datetime+"");
json_to_parse=GetService.getHttpJSON( http+""+type+"?id=”+id+"&datetime="+datetime
+"");
if(json_to_parse= =null)
return null;
try{
JSONObject j_root = new JSONObject(json_to_parse);
JSONArray j_msglist = (JSONArray)j_root.get("airmeslist");
for( int i = O; i<j_msglist.length(); + + i){
JSONObject j_msg = (JSONObject) j_msglist.get(i);
AirMes msg = new AirMes( );
msg.setMoteid_ID(j_msg.getString(" moteid_ID" ));
msg.setTimestampArrive_TM( j_msg.getString( "timestampArrive_TM"));
msg.setTemperature_AVG( j_msg.getString( "temperature_AVG"));
msg.setHumidity_AVG( j_msg.getString( "humidity_AVG"));
msg.setLight_AVG( j_msg.getString( "light_AVG" )) ;
msg_list.add( msg);
}
} catch (JSONException e) {
e.printStackTrace( );
return null;
}
//为数据传输对象填充成员对象
mag_list_ json.setMsg_list( msg_list);
//返回数据传输对象
return msg_list_json;
}
//type: dayavgco2mes 过去24小时 weekavgco2mes 过去7天 monthavgco2mes 过去30天 yearavgco2mes 过去12个月
public static C02ListJson getData( C02ListJson msg_list_json, String http, String id, long datetime, String type) {
//为数据传输对象初始化成员对象
ArrayList<C02Mes> msg_list = new ArrayList<C02Mes> ( );
msg_list_json = new C02ListJson( );
//解析json并填入对应成员对象
json_to_parse = GetService. getHttpJSON( http + "" + type + "?id = " + id + " &datetime = " + datetime+"");
if(json_to_parse = = null)
return null;
try {
JSONObject j_root = new JSONObject(json_to_parse);
JSONArray j_msglist = (JSONArray) j_root.get("co2meslist");
for(int i = O; i<j_msglist.length(); + + i) {
JSONObject j_msg =(JSONObject)j_msglist. get(i);
C02Mes msg = new C02Mes ( );
msg.setMoteid_ID( j_msg. getString( "moteid_ID" ));
msg.setTimestampArrive_TM( j_msg.getString( "timestampArrive_TM" ));
msg.setC02_AVG(j_msg.getString( "C02_AVG"));
msg_list.add(msg);
}
} catch (JSONException e){
e.printStackTrace();
return null;
}
//为数据传输对象填充成员对象
msg_list_json .setMsg_list(msg_list);
//返回数据传输对象
return msg_list_json;
}
public static C02ListJson getDate(CO2ListJosn msg_list_json,String http, double longitude double latitude,long datetime){
//为数据传输对象初始化成员对象
ArrayList<002Mes>msg_list=new ArrayList<CO2Mes>();
//解析json并填入对应成员对象
json_to_parse = GetService. getHttpJSON( http + "areaco2mes?longitude = "+ longitude + " &latitude= " + latitude + " &datetime = " + datetime + " " ) ;
if ( json_to_parse = = null)
return null;
try {
JSONObject j_root = new JSONObject(json_to_parse);
JSONArray j_msglist = (JSONArray) j_root. get("co2meslist");
for ( int i = 0; i< j_msglist. length(); + + i) {
JSONObject j_msg = (JSONObject) j_msglist. get(i);
C02Mes msg = new C02Mes( );
msg. setMoteid_ID( j_msg. getString( "Moteid_ID")) ;
msg.setTimestampArrive_TM ( j_msg. getString( " TimestampArrive_TM" )) ;
msg. setC02_ AVG(j_msg.getString( "C02_ AVG"));
msg_list.add( msg);
}
}catch (JSONException e){
e.printStackTrace();
return null;
}
//为数据传输对象填充成员对象
msg_list_j son.setMsg_list (msg_list);
//返回数据传输对象
return msg_list_j son;
}
public static AirListcjson getData(AirListjson msg_list_ json,String http,double longitude, double latitude, long datetime) {
//为数据传输对象初始化成员对象
ArrayList<AirMes> msg_list=new ArrayList<AirMes>();
//解析Json并填人对应成员对象
json_to_parse = GetService.getHttpJSON( http + "areaairmes?longitude = " + longitude + " &latitude=" + latitude + " &datetime = " + datetime + " " ) ;
if ( json_to_parse = = null)
return null;
try {
JSONObject j_root = new JSONOlbject(json_to_parse);
JSONArray j_msglist = (JSONArray) j_root.get("airmeslist");
for (int i = 0; i< j_msglist. length(); + + i) {
JSONObject j_msg= (JSONObject) j_msglist. get(i);
AirMes msg = new AirMes( ) ;
msg.setMoteid_ID( j_msg. getString( "moteid_ ID" )) ;
msg.setTimestampArrive_ TM( j_msg.getString( "timestampArrive_TM"));
msg.setTemperature_ AVG( j_msg.getString( "temperature_AVG" )) ;
msg.setHumidity_AVG( j_msg.getString( "humidity_AVG"));
msg.setLight_AVG( j_msg.getString("light_ AVG"));
msg_list.add( msg);
}
} catch (JSONException e){
e.printStackTrace();
retrun null;
}
//为数据传输对象填充成员对象
msg_list_json.setMsg_list( msg_list);
//返回数据传输对象
return msg_list_json;
}
ublic static NodeListjsan getData(NodeListjson msg_list_json, String http, double longitude, double latitude, int r) {
//为数据传输对象初始化成员对象
ArrayList<NodeInfoForJson> msg_list= new ArrayList<NodeInfoForJson> ( ) ;
//解析json并填入对应成员对象
json_ to_parse = GetService. getHttpJSON ( http + " nodelist? longitude = " + longitude + " &latitude +"&r="+r+"");
if ( json_to_parse = = null)
return null;
try {
JSONObject j_root = new JSONObject(json_to_parse);
JSONArray j_msglist = (JSONArray) j_root. get("nodelist");
for (int i = O; i< j_msglist. length(); + + i) {
JSONObject j_msg = (JSONObject) j_msglist. get(i);
NodeInfoForjson msg= new NodeInfoForJson( );
int nodeID=j_msg.getInt("moteid_ID");
msg.setMoteid_ID(nodeID);
msg.setMoteid_Type((nodeID %1000)>800?2:1);
msg.setLongitude(j_msg.getDouble("longitude"));
msg.setLatitude(j_msg.getDouble("latitude"));
msg_list,add(msg);
}
}catch(JSONException e){
e.printStackTrace();
return null;
//为数据传输对象填充成员对象
msg_list_json,setNode_list(msg_list);
//返回数据传输对象
return msg_1ist_json;
}
public static NodeListJson getData(NodeListJson msg_ list—json,String search_
url){
//为数据传输对象初始化成员对象
ArrayList<NodeInfaForJson>msg_list=new ArrayList<NodeInforJson>();
//解析json并填入对应成员对象
json_to_parse=GetService. getHttpJson( http+search_url);
if( json_to_parse = =null)
return null;
try{
JSONObject j_root=new JSONObject(json_to_parse);
JSONArray j_msglist= (JSONArray) j_root. get( "nodelist");
for(int i= O;i<:j_msglist. length(); + +i){
JSONObject j_msg=(JSONObject) j_msglist. get(i);
NodelnfoForJson msg = new NodeInfoForJson();
int nodeID = j_msg. getInt( "moteid_ID" ) ;
msg.setMoteid _ID( nodeID) ;
msg.setMoteid _ Type (( nodeID %1000)>800? 2 : 1) ;
msg.setLongitude( j_msg. getDouble( "longitude" ));
msg.setLatitude( j_msg. getDouble( " latitude" ));
msg_list.add(msg);
}
} catch (JSONException e){
e.printStackTrace();
return null;
}
//为数据传输对象填充成员对象
msg_list_json.setNode_list(mag_list);
//返回数据传输对象
return msg_list_json;
}
public static AirMes getData(AirMes msg, String http,String url){
//解析json并填入对应成员对象
Json_to_parse=GetService.getHttpjSON( http+url);
if(json_ to_parse= =null)
return null;
try{
JSONObject j_root=new JSONObject(json_ to_parse);
JSONObject j_msg=(JSONObject) j_root. get("airmes");
msg. setTimestampArrive_TM( j_msg. getString( "timestampArrive_ TM" )) ;
msg. setTemperature_AVG( j_msg. getString( "temperature AVG")) ;
msg. setHumidity_AVG( j_msg. getString( " humidity_AVG")) ;
msg. setLight_AVG( j_msg. getString( "light_AVG" )) ;
} catch (JSONException e) {
e.printStackTrace ( ) ;
return null;
}
//返回数据传输对象
return msg;
}
public static C02Mes getData(C02Mes msg, String http, String url) {
//解析 json并填入对应员对象
json_to_parse = GetService. getHttpJSON( http + url) ;
if ( json_to_parse= = null)
{
System. out. println( http + url) ;
return null;
}
try {
JSONObject j_root = new JSONObject(json to_parse);
JSONObject j_msg = (JSONObject) j_root. get("co2mes");
msg. setTimestampArrive_TM( j_msg. getString( "timestampArrive_TM") ) ;
msg. setC02_AVG(j_msg. getString("C02_ AVG"));
} catch (JSONException e) {
e. printStackTrace( ) ;
return null;
}
//返回数据传输对象
return msg;
}