android解析xml文件 Android(安卓)DOM解析XML之全球实时地震信息列表
16lz
2021-12-04
- publicclassHttpGetextendsActivity{
- privateListViewlist;
- EarthQuakeInfoselectedQuake;
- ArrayAdapteradapter;
- ArrayListinfoList=newArrayList();
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.main);
- //设置listView的内容为infoList
- list=(ListView)this.findViewById(R.id.list);
- adapter=newArrayAdapter(this,android.R.layout.simple_list_item_1,infoList);
- //设置ListView的适配器为adapter
- list.setAdapter(adapter);
- getInfo();//获得infoList的具体内容。
- }
- privatevoidgetInfo(){
- URLurl;
- try{
- Stringfeed=getString(R.string.feed);
- url=newURL(feed);
- URLConnectionconnection=url.openConnection();
- HttpURLConnectionhttpConnection=(HttpURLConnection)connection;
- intresponseCode=httpConnection.getResponseCode();
- if(responseCode==HttpURLConnection.HTTP_OK){
- InputStreamin=httpConnection.getInputStream();
- DocumentBuilderFactorydbfactory=DocumentBuilderFactory.newInstance();
- DocumentBuilderdb=dbfactory.newDocumentBuilder();
- //解析地震feed
- Documentdom=db.parse(in);
- ElementdocEle=dom.getDocumentElement();
- //清空旧的地震信息
- infoList.clear();
- //获得地震信息列表
- NodeListnl=docEle.getElementsByTagName("entry");
- if(nl!=null&&nl.getLength()>0){
- for(inti=0;i<nl.getLength();i++){
- Elemententry=(Element)nl.item(i);
- Elementtitle=(Element)entry.getElementsByTagName("title").item(0);
- Elementgeo=(Element)entry.getElementsByTagName("georss:point").item(0);
- Elementwhen=(Element)entry.getElementsByTagName("updated").item(0);
- Stringdetails=title.getFirstChild().getNodeValue();
- Stringpoint=geo.getFirstChild().getNodeValue();
- Stringdate=when.getFirstChild().getNodeValue();
- SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
- Dateqdate=newGregorianCalendar(0,0,0).getTime();
- try{
- qdate=sdf.parse(date);
- }catch(Exceptione){
- e.printStackTrace();//TODO:handleexception
- }
- String[]location=point.split("");
- Locationloc=newLocation("dummyGPS");
- loc.setLatitude(Double.parseDouble(location[0]));
- loc.setLongitude(Double.parseDouble(location[1]));
- StringmagnitudeString=details.split("")[1];
- intend=magnitudeString.length()-1;
- doublemagnitude=Double.parseDouble(magnitudeString.substring(0,end));
- details=details.split(",")[1].trim();
- EarthQuakeInfoinfo=newEarthQuakeInfo(qdate,details,loc,magnitude);
- //处理地震信息
- newEntry(info);
- }
- }
- }
- }catch(Exceptione){
- //TODO:handleexception
- }
- }
- privatevoidnewEntry(EarthQuakeInfoinfo){
- infoList.add(info);
- adapter.notifyDataSetChanged();
- }
- publicclassEarthQuakeInfo{
- publicDatedate;
- publicStringdetails;
- publicLocationlocation;
- publicdoublemagnitude;
- publicEarthQuakeInfo(Dated,Stringde,Locationloc,doublemag){
- this.date=d;
- this.details=de;
- this.location=loc;
- this.magnitude=mag;
- }
- @Override
- publicStringtoString(){
- SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd''hh:mm:ss");
- returnsdf.format(date)+"\n里氏"+magnitude+"级"+details+"地点"+location;
- }
- }
- }
更多相关文章
- android系统信息总结
- Android中读取短信信息
- 取消自动获得焦点和弹出对话框
- Android(安卓)谷歌统计
- 获得位置信息 android
- android中操纵sqlite数据库
- Android(安卓)Studio的build.gradle里面的各种版本信息
- HttpUrlConnect 响应为空的问题分析
- execlp启动android进程命令窗口通过adb shell 进入android 的Lin