zoukankan      html  css  js  c++  java
  • android----pull解析方式

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

    复制代码
      1 package com.hb.xml;
      2 
      3 import java.io.InputStream;
      4 import java.net.HttpURLConnection;
      5 import java.net.URL;
      6 
      7 import org.xmlpull.v1.XmlPullParser;
      8 
      9 import android.app.Activity;
     10 import android.os.Bundle;
     11 import android.os.Handler;
     12 import android.os.Message;
     13 import android.util.Xml;
     14 import android.view.View;
     15 import android.widget.Button;
     16 import android.widget.EditText;
     17 import android.widget.TextView;
     18 import android.widget.Toast;
     19 
     20 public class MainActivity extends Activity {
     21     protected static final int NUMBER = 0;
     22     protected static final int LOCATION = 1;
     23     protected static final int PHONEJX = 2;
     24     protected static final int NO = 3;
     25     private Button bt_start;
     26     private TextView tv_desc;
     27     private TextView tv_number;
     28     private TextView tv_for;
     29     private String path;
     30     private XmlPullParser xml;
     31     private Handler handler=new Handler(){
     32         public void handleMessage(android.os.Message msg) {
     33             switch (msg.what) {
     34             case NUMBER:
     35                 tv_number.setText((String)msg.obj);
     36                 Toast.makeText(MainActivity.this, "测试成功", 0).show();
     37                 break;
     38             case LOCATION:
     39                 tv_for.setText((String)msg.obj);
     40                 Toast.makeText(MainActivity.this, "测试成功", 0).show();
     41                 break;
     42             case PHONEJX:
     43                 tv_desc.setText((String)msg.obj);
     44                 Toast.makeText(MainActivity.this, "测试成功", 0).show();
     45                 break;
     46             case NO:
     47                 Toast.makeText(MainActivity.this, "测试失败", 0).show();
     48                 break;
     49             }
     50         };
     51     };
     52 
     53     @Override
     54     protected void onCreate(Bundle savedInstanceState) {
     55         super.onCreate(savedInstanceState);
     56         setContentView(R.layout.activity_main);
     57         initView();
     58     }
     59 
     60     private void initView() {
     61         bt_start=(Button) findViewById(R.id.bt_start);
     62         tv_desc=(TextView) findViewById(R.id.tv_desc);
     63         tv_number=(TextView) findViewById(R.id.tv_number);
     64         tv_for=(TextView) findViewById(R.id.tv_for);
     65     }
     66     public void testtesting(View v){
     67         new Thread(){
     68 
     69             public void run() {
     70                 try {
     71                     URL url = new URL("http://192.168.1.104:8080/test.xml");
     72                     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     73                     conn.setConnectTimeout(3000);
     74                     conn.setRequestMethod("GET");
     75                     int code = conn.getResponseCode();
     76                     if(code==200){
     77 
     78                         InputStream is = conn.getInputStream();
     79                         xml = Xml.newPullParser();
     80                         xml.setInput(is, "gbk");
     81                         int type = xml.getEventType();
     82                         while (type!=XmlPullParser.END_DOCUMENT) {
     83                             if(type == XmlPullParser.START_TAG){
     84                                 if("phonenum".equals(xml.getName())){
     85                                     String phonenum=xml.nextText();
     86                                     Message msg= new Message();
     87                                     msg.what=NUMBER;
     88                                     msg.obj=phonenum;
     89                                     handler.sendMessage(msg);
     90                                 }else if("location".equals(xml.getName())){
     91                                     String location=xml.nextText();
     92                                     Message msg= new Message();
     93                                     msg.what=LOCATION;
     94                                     msg.obj=location;
     95                                     handler.sendMessage(msg);
     96                                 }else if("phoneJx".equals(xml.getName())){
     97                                     String desc=xml.nextText();
     98                                     Message msg= new Message();
     99                                     msg.what=PHONEJX;
    100                                     msg.obj=desc;
    101                                     handler.sendMessage(msg);
    102                                 }
    103                             }else{
    104                                 //测试失败
    105                             }
    106                             type=xml.next();
    107                         }
    108                     }
    109                 } catch (Exception e) {
    110                     e.printStackTrace();
    111                     Message msg= new Message();
    112                     msg.what=NO;
    113                     handler.sendMessage(msg);
    114                 }
    115             };
    116         }.start();
    117     }
    118 }
  • 相关阅读:
    vue学习03 v-html
    [spring guides]网关入门
    记一次公司mssql server密码频繁被改的事件
    重构 maixpy 的 board_info + config.json 从而自适应硬件版型。
    介绍 MaixUI 系列(一)如何食用?
    (旧文)在 micropython / esp-at / arduino 中实现 软串口(software-serial) 的参考
    以优化 MaixPy 的启动速度为例,说说 K210 的双核使用及原子操作。
    我是如何在 Win + VSCode 上开发 Keil for GD32 实现 I2C 从机的游戏机手柄。
    为 MaixPy 加入软 SPI 接口(移植 MicroPython 的 SPI)
    为 MaixPy 加入软 I2C 接口(移植 MicroPython 的 I2C)
  • 原文地址:https://www.cnblogs.com/hrzgj/p/14941905.html
Copyright © 2011-2022 走看看