解释通讯协议中的xml

现在做的应用基本离不网络,数据协议一般是使用 轻量级的 json 、xml,这篇文章分享一个解释xml的功能

方便从网络的字节流中提取字符串数据。

代码如下:

public class XMLLoader {        
    public interface OnLoadXMLCompletedListener{
        public void onLoadXMLCompleted(Object obj, int what,Bundle data);
    }
    
    private static final String TAG = "XMLLoader";
    
    private XMLLoaderHandler mHandler;
    private Handler mHListener;
    private OnLoadXMLCompletedListener mOnLoadXMLCompletedListener;
    private String mUrl;
    private int mWhat;
    private Bundle mData;

    public void load(String url, XMLLoaderHandler handler, OnLoadXMLCompletedListener listener, int what,Bundle data) {
        mUrl = url;
        mHandler = handler;
        mOnLoadXMLCompletedListener = listener;
        mWhat = what;
        mData = data;
        Util.log(TAG, "url = "+mUrl);
        
        URLLoader loader = new URLLoader();
        loader.loadUrl(url, new URLLoader.OnLoadFinishedListener() {
            
            @Override
            public void onFinish(InputStream is) {
//                Util.log(TAG, "Func:onFinish, Thread name = " + Thread.currentThread().getName() + ", ID = " + Thread.currentThread().getId() + ", state = " + Thread.currentThread().getState());
                parser(is);                
            }
        });
    }
    
    public void load(String url, XMLLoaderHandler handler, Handler listener, int what,Bundle data) {
        mUrl = url;
        mHandler = handler;
        mHListener = listener;
        mWhat = what;
        mData = data;
        Util.log(TAG, "url = "+mUrl);
        
        URLLoader loader = new URLLoader();
        loader.loadUrl(url, new URLLoader.OnLoadFinishedListener() {
            
            @Override
            public void onFinish(InputStream is) {
                parser(is);                
            }
        });
    }
    public void load(InputStream is,XMLLoaderHandler handler, Handler listener, int what,Bundle data){
        mHandler = handler;
        mHListener = listener;
        mWhat = what;
        mData = data;
        Util.log(TAG, "InputSream = " + is);
        if(is != null)
            parser(is);
    }
    public void load(InputStream is, XMLLoaderHandler handler, OnLoadXMLCompletedListener listener, int what,Bundle data) {
        mHandler = handler;
        mOnLoadXMLCompletedListener = listener;
        mWhat = what;
        mData = data;
        Util.log(TAG, "InputSream = " + is);
        if(is != null)
            parser(is);
    }
    public Object load(InputStream is){
        if(is != null){
            return parser(is);
        }else
            return null;
    }
    
    public Object load(byte[] body,XMLLoaderHandler handler, OnLoadXMLCompletedListener listener){
        mHandler = handler;
        mOnLoadXMLCompletedListener = listener;
        InputStream is=new ByteArrayInputStream(body);
        if(is!=null){
            return parser(is);
        }else 
            return null;
    }
    public Object load(InputStream is,XMLLoaderHandler handler){
        mHandler = handler;

        Util.log(TAG, "InputSream = " + is);
        if(is != null)
            return parser(is);
        return null;
    }

    private Object parser(InputStream is) {
         SAXParserFactory factory=SAXParserFactory.newInstance(); 
         Object obj = null;
         try {//解释xml数据            
             SAXParser parser=factory.newSAXParser();              
             XMLReader xmlReader=parser.getXMLReader();                     
             xmlReader.setContentHandler(mHandler);   
             InputSource input=new InputSource();
             input.setEncoding("utf-8");
             input.setByteStream(is);
             xmlReader.parse(input);
             obj = mHandler.getInfo();
             Util.log(TAG, "[parser] obj = "+obj);
         } catch(Exception ex) {
             ex.printStackTrace();
         }
//         Util.log(TAG, "Func:parser, Thread name = " + Thread.currentThread().getName() + ", ID = " + Thread.currentThread().getId() + ", state = " + Thread.currentThread().getState());
         if(null != mOnLoadXMLCompletedListener) {//通知UI线程更新数据
             mOnLoadXMLCompletedListener.onLoadXMLCompleted(obj, mWhat,mData);
            
         } else if(null != mHListener){
             Message msg = mHListener.obtainMessage(mWhat);
             if(mData != null)
                 msg.setData(mData);
             msg.obj = obj;
             msg.sendToTarget();
         }
         return obj;
    }    
}

 

更多的移动互联网的发展趋势app开发移动互联网应用相关的资料请到互联网的一点事www.yidin.net 留言

android QQ群:222392467

资料:

http://www.yidin.net/?p=8280

http://www.yidin.net/?p=9725

posted on 2013-05-09 10:53  梁DH  阅读(1152)  评论(1编辑  收藏  举报

导航