android之网络编程解析XML

不多说直接上代码


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;
     //控件
ListView 
private 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;
}


然后我这里是模仿安卓底层的做法可能比较麻烦


大概就是这样



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值