zoukankan      html  css  js  c++  java
  • Pull方式解析XML文件

    package com.pingyijinren.test;
    
    import android.content.Intent;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserFactory;
    
    import java.io.BufferedReader;
    import java.io.EOFException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.StringReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class MainActivity extends AppCompatActivity{
        private Button button;
        private TextView textView;
        private static final int SHOW_RESPONSE=0;
        private Handler handler=new Handler(){
            public void handleMessage(Message message){
                switch(message.what){
                    case SHOW_RESPONSE:
                        String response=(String)message.obj;
                        textView.setText(response);
                }
            }
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button=(Button)findViewById(R.id.button);
            textView=(TextView)findViewById(R.id.textView);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendRequestWithHttpURLConnection();
                }
            });
        }
    
        private void sendRequestWithHttpURLConnection(){
            new Thread(new Runnable(){
                @Override
                public void run(){
                    try{
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet=new HttpGet("http://172.29.209.1/get_data.xml");
                        HttpResponse httpResponse=httpClient.execute(httpGet);
                        if(httpResponse.getStatusLine().getStatusCode()==200){
                            HttpEntity httpEntity=httpResponse.getEntity();
                            String response= EntityUtils.toString(httpEntity,"utf-8");
    
                            Log.d("MainActivity",response);
    
                            parseXMLWithPull(response);
                        }
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
    
    
                }
            }).start();
        }
    
        private void parseXMLWithPull(String xmlData){
            try{
                XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
                XmlPullParser xmlPullParser=factory.newPullParser();
                xmlPullParser.setInput(new StringReader(xmlData));
                int eventType=xmlPullParser.getEventType();
    
                String id="";
                String name="";
                String version="";
    
                while(eventType!=xmlPullParser.END_DOCUMENT){
                    String nodeName=xmlPullParser.getName();
    
                    switch(eventType){
                        case XmlPullParser.START_TAG:{
                            if(nodeName.equals("id")){
                                id=xmlPullParser.nextText();
                            }
                            else if(nodeName.equals("name")){
                                name=xmlPullParser.nextText();
                            }
                            else if(nodeName.equals("version")){
                                version=xmlPullParser.nextText();
                            }
                            break;
                        }
    
                        case XmlPullParser.END_TAG:{
                            if(nodeName.equals("app")){
                                Log.d("MainActivity ","id"+id);
                                Log.d("MainActivity","name"+name);
                                Log.d("MainActivity","version"+version);
                            }
                        }
                        break;
    
                        default:
                            break;
                    }
                    eventType=xmlPullParser.next();
                }
    
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    轻松管理您的网络password
    尝到awk
    重载虚函数的相关问题
    阿赫亚web安全JSON
    SplitContainer如何实现左侧导航,正确显示和导航内容
    POJ 3450 Corporate Identity KMP解决问题的方法
    virtio-blk分析
    JavaScript权威指南科03章 种类、值和变量(1)
    iOS开展-Xcode技巧总结(持续更新)
    POI设置边框
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5508200.html
Copyright © 2011-2022 走看看