android之网络编程解析XML
来源:互联网 发布:org.apache.axis 编辑:程序博客网 时间:2024/06/06 07:08
不多说直接上代码
import android.app.ProgressDialog;import android.os.AsyncTask;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;public class MainActivity extends AppCompatActivity { private ProgressDialog progressDialog;
//控件ListViewprivate ListView lv_main_list; //FG为实体类
private List<FG> list=new ArrayList<>(); private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressDialog = new ProgressDialog(this); progressDialog.setMessage("正在拼命loading中..."); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int i, View view, ViewGroup parent) { if(view==null){ view= LayoutInflater.from(MainActivity.this).inflate(R.layout.item_listview,null); ItemTag itemTag=new ItemTag(); itemTag.tv_content= (TextView) view.findViewById(R.id.tv_item_listview_content); itemTag.tv_name= (TextView) view.findViewById(R.id.tv_item_listview_name); itemTag.tv_time= (TextView) view.findViewById(R.id.tv_item_listview_time); view.setTag(itemTag); } ItemTag itemTag= (ItemTag) view.getTag(); itemTag.tv_name.setText(list.get(i).getName()); itemTag.tv_content.setText(list.get(i).getContent()); itemTag.tv_time.setText(list.get(i).getTime()); return view; } } public void getXML(View view){ new MyTask().execute(); }class MyTask extends AsyncTask{ @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.show(); } @Override protected Object doInBackground(Object[] params) { List<FG> fqs=new ArrayList<>(); //获取网络数据 //01.定义获取网络的数据的路径 String path=""; try { //02.实例化Url URL url=new URL(path); //03.获取连接对象 HttpURLConnection con=(HttpURLConnection) url.openConnection(); //04.设置请求方式 con.setRequestMethod("GET"); //05.设置请求连接超时的时间 con.setConnectTimeout(3000); //06.获取响应码 int code=con.getResponseCode(); if(code==200){ //07.获取返回过来的数据(XML) InputStream is=con.getErrorStream(); //08.测试(删除-注释) //缓冲字符流 String str=null;// BufferedReader br=new BufferedReader(new InputStreamReader(is));// while((str=br.readLine())!=null){// Log.i("test",str);// } //09.解析XML DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder(); Document document=documentBuilder.parse(is); //获取跟标签 Element root=document.getDocumentElement(); //Node Element //标签名 NodeList nodeList=root.getElementsByTagName("fq"); for(int i=0;i<nodeList.getLength();i++){ Element element= (Element) nodeList.item(i); String name=element.getAttribute("name"); //获取子标签<content><time>//标签名Element elementContent= (Element) element.getElementsByTagName("content").item(0); String content=elementContent.getTextContent();//标签名Element elementTime= (Element) element.getElementsByTagName("time").item(0); String time=elementTime.getTextContent(); Log.i("test",name+" "+content+" "+time); FG fq=new FG(name,content,time); fqs.add(fq); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } return fqs; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); List<FG> fqs= (List<FG>) o; list.addAll(fqs); myAdapter.notifyDataSetChanged(); progressDialog.cancel(); }}}
FG实体类
/** * Created by YaozzzzMABY on 2017/2/24. */public class FG { private String name; private String content; private String time; public FQ() { super(); } public FQ(String name, String content, String time) { super(); this.name = name; this.content = content; this.time = time; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTime() { return time; } public void setTime(String time) { this.time = time; }}
对了还要一个展示数据的界面
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/tv_item_listview_name" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/tv_item_listview_content" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/tv_item_listview_time" />
还有用于绑定控件的类
import android.widget.TextView;/** * Created by YaozzzzMABY on 2017/2/24. */public class ItemTag { public TextView tv_name; public TextView tv_content; public TextView tv_time;}然后我这里是模仿安卓底层的做法可能比较麻烦
大概就是这样
0 0
- Android网络编程之sax解析xml
- Android网络编程之pull解析xml
- Android网络编程之XML解析
- android之网络编程解析XML
- android网络编程--xml解析
- Android网络编程之二:DOM解析XML文件
- Android网络编程之三:XmlPullParser解析Xml文件
- Android 网络编程之 XML 和 JSON 解析
- Android网络编程之解析XML格式数据
- Android网络编程之Pull解析XML文件
- 网络编程之android后台取服务器端xml解析之dom解析
- Android 网络获取数据之XML解析
- Android中级之网络数据解析一之xml解析
- 网络解析之xml
- Android网络编程之WebView【实例解析】
- Android网络编程之HttpUrlConnection【实例解析】
- Android网络编程之JSON解析
- Android网络之数据解析----SAX方式解析XML数据
- 函数
- EffectiveJava(10)覆盖equals是视情况覆盖toString
- 【vijos1286】座位安排
- 游戏UI框架设计(二)
- PHP 7 的五大新特性
- android之网络编程解析XML
- Battleships in a Board 题解
- PHP7发布。最好语言发布了新版本
- Linux编程手册学习笔记
- C# 自定义用户控件
- EffectiveJava(11)Java中的clone
- 我的第一份博客
- 【bzoj 1072】[SCOI2007]排列perm
- 配置本地yum源