Android学习——pull解析方式

在Android中,常见的XML解析器分别为SAX解析器、DOM解析器和PULL解析器,其中PULL解析器小巧轻便,解析速度快,简单易用,非常适合在Android移动设备中使用,Android系统内部在解析各种XML时也是用PULL解析器,今天我来介绍一下PULL解析器

package com.hb.xml;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    protected static final int NUMBER = 0;
    protected static final int LOCATION = 1;
    protected static final int PHONEJX = 2;
    protected static final int NO = 3;
    private Button bt_start;
    private TextView tv_desc;
    private TextView tv_number;
    private TextView tv_for;
    private String path;
    private XmlPullParser xml;
    private Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case NUMBER:
                tv_number.setText((String)msg.obj);
                Toast.makeText(MainActivity.this, "测试成功", 0).show();
                break;
            case LOCATION:
                tv_for.setText((String)msg.obj);
                Toast.makeText(MainActivity.this, "测试成功", 0).show();
                break;
            case PHONEJX:
                tv_desc.setText((String)msg.obj);
                Toast.makeText(MainActivity.this, "测试成功", 0).show();
                break;
            case NO:
                Toast.makeText(MainActivity.this, "测试失败", 0).show();
                break;
            }
        };
    };

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

    private void initView() {
        bt_start=(Button) findViewById(R.id.bt_start);
        tv_desc=(TextView) findViewById(R.id.tv_desc);
        tv_number=(TextView) findViewById(R.id.tv_number);
        tv_for=(TextView) findViewById(R.id.tv_for);
    }
    public void testtesting(View v){
        new Thread(){

            public void run() {
                try {
                    URL url = new URL("http://192.168.1.104:8080/test.xml");
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(3000);
                    conn.setRequestMethod("GET");
                    int code = conn.getResponseCode();
                    if(code==200){

                        InputStream is = conn.getInputStream();
                        xml = Xml.newPullParser();
                        xml.setInput(is, "gbk");
                        int type = xml.getEventType();
                        while (type!=XmlPullParser.END_DOCUMENT) {
                            if(type == XmlPullParser.START_TAG){
                                if("phonenum".equals(xml.getName())){
                                    String phonenum=xml.nextText();
                                    Message msg= new Message();
                                    msg.what=NUMBER;
                                    msg.obj=phonenum;
                                    handler.sendMessage(msg);
                                }else if("location".equals(xml.getName())){
                                    String location=xml.nextText();
                                    Message msg= new Message();
                                    msg.what=LOCATION;
                                    msg.obj=location;
                                    handler.sendMessage(msg);
                                }else if("phoneJx".equals(xml.getName())){
                                    String desc=xml.nextText();
                                    Message msg= new Message();
                                    msg.what=PHONEJX;
                                    msg.obj=desc;
                                    handler.sendMessage(msg);
                                }
                            }else{
                                //测试失败
                            }
                            type=xml.next();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg= new Message();
                    msg.what=NO;
                    handler.sendMessage(msg);
                }
            };
        }.start();
    }
}

 

上一篇:docker常用命令


下一篇:DevOps使用教程 华为云(10)GitHub git Pull Request 合并请求