使用网络XML,pull解析xUtils框架

1.首先xUtils-2.4.4.jar包导入libs:


2.实现的.java文件有:


3.代码展示:
    3.1、bean.class类

public class Bean {
    public String title;
    public String body;

    public Bean() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Bean(String title, String body) {
        super();
        this.title = title;
        this.body = body;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "Bean [title=" + title + ", body=" + body + "]";
    }

}


  3.2 MainActivity.class类

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Xml;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class MainActivity extends Activity {
    String path = "http://172.17.29.120/localuser/loupengfei/kaoshi/student.xml";
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化控件
        initCtrl();
        // 获取数据源
        getDatas();
    }

    private void initCtrl() {
        tv = (TextView) findViewById(R.id.tv);

    }

    private void getDatas() {

        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {
            private Bean bean;

            // 获取数据失败
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub

            }

            // 获取数据成功
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {

                String json = arg0.result;
                // String 转换成流
                InputStream inputStream = new ByteArrayInputStream(json
                        .getBytes());
                // 获得pull解析对象
                XmlPullParser newPullParser = Xml.newPullParser();

                try {
                    // 设置输入流
                    newPullParser.setInput(new InputStreamReader(inputStream));
                    // 得到标签的类型
                    int eventType = newPullParser.getEventType();
                    ArrayList<Bean> list = new ArrayList<Bean>();
                    while (eventType != XmlPullParser.END_DOCUMENT) {
                        switch (eventType) {
                        case XmlPullParser.START_DOCUMENT:
                            System.out.println("开始文件标签");
                            break;
                        case XmlPullParser.START_TAG:
                            System.out.println("开始标签");
                            String tagName = newPullParser.getName();
                            if (tagName.equals("student")) {
                                bean = new Bean();

                            } else if (tagName.equals("name")) {
                                String title = newPullParser.nextText();
                                bean.title = title;
                                System.out.println("address"
                                        + "--------------------" + title);
                            } else if (tagName.equals("address")) {
                                String body = newPullParser.nextText();
                                bean.body = body;
                                System.out
                                        .println("body"
                                                + "____________________________"
                                                + body);
                            }
                            break;
                        case XmlPullParser.END_TAG:
                            System.out.println("开始文件标签");
                            tagName = newPullParser.getName();
                            if (tagName.equals("student")) {
                                list.add(bean);
                            }
                            break;

                        default:
                            break;
                        } // 获得下一个标签类型勿忘
                        eventType = newPullParser.next();
                    }
                    String result = "";
                    for (Bean b : list) {    
                        result += b.toString();    
                    }    
                     tv.setText(result);  
                    System.out.println(list.toString());

                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        });
    }
}


 还有 注意一点别忘了加网络权限

  <uses-permission android:name="android.permission.INTERNET" />

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值