不多说直接上代码
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;
//控件
private ListView lv_main_list; //FG为实体类ListView
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; }
然后我这里是模仿安卓底层的做法可能比较麻烦
大概就是这样