温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

解释通讯协议中的xml

发布时间:2020-06-28 10:21:29 来源:网络 阅读:588 作者:yidinshi 栏目:开发技术

现在做的应用基本离不网络,数据协议一般是使用 轻量级的 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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI