当前位置: 首页 > 知识库问答 >
问题:

使用kxml.zip的Android XML解析代码

傅振濂
2023-03-14

我正在使用这段代码解析一个xml文件,我有url地址,但它给我的错误是android os网络上的主线程异常在这句话URLConnection httpConnection=url.openconnection();inputStream=HttpConnection.GetInputStream();下面是我的代码:

package com.example.mainp;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;

import org.apache.http.HttpConnection;
import org.kxml.Xml;
import org.kxml.parser.XmlParser;
import org.kxml.parser.ParseEvent;
import org.kxml.parser.XmlParser;

import android.app.Activity;
import android.widget.Toast;






public class parse {

     public static Vector places;
     Activity ac;
    Place place;
      private ParseEvent event = null;
    public void init(Activity activity) {
        this.ac=activity;
        XmlParser parser = null;
        InputStreamReader isR = null;
        HttpConnection connection = null;
        try {

            InputStream inputStream = null;
            System.out.println("0");
            Toast.makeText(ac, "24", Toast.LENGTH_LONG).show();
            //URL url = new URL("http://217.52.114.3/Mobinil%20Islamic%20WebService/MobinilIslamicService.asmx/GetIslamicDirectory");
            URL url= new URL("http://217.52.114.3/CinemaGuid/CinemaGuid.asmx/getTVSchedule");
            System.err.println("2");
            URLConnection httpConnection = url.openConnection();
            inputStream = httpConnection.getInputStream();
//          inputStream = connection.openInputStream();
            Toast.makeText(ac, "4", Toast.LENGTH_LONG).show();  
            isR = new InputStreamReader(inputStream, "UTF-8");
            System.out.println("3");
            parser = new XmlParser(isR);
            System.out.println("4");
            Toast.makeText(activity, "5", Toast.LENGTH_LONG).show();    
            places = new Vector<Place>();
            traverse(parser);
            isR.close();
            inputStream.close();
            inputStream = null;
            isR = null;
            // System.gc();
        } catch (Exception ex) {
            Toast.makeText(activity, ex.toString(), Toast.LENGTH_LONG).show();  
        }
    }
    private void traverse(XmlParser parser) {
        // TODO Auto-generated method stub
        boolean leave = false;
        do {
            try {
                event = parser.read();
                ParseEvent pe;
                switch (event.getType()) {
                    case Xml.START_TAG:
                        // see API doc of StartTag for more access methods
                        // Pick up Title for display
                        if("places".equalsIgnoreCase(event.getName())){
                            places = new Vector();
                            //Toast.makeText(ac, "6", Toast.LENGTH_LONG).show();    
                        }
                        if("places".equalsIgnoreCase(event.getName())){
                            place = new Place();
                            place.setName(event.getValue("name"));
                            place.seturl(event.getValue("url"));
                            place.setaddress(event.getValue("adress"));
                            //Toast.makeText(MainPActivity.this, "7", Toast.LENGTH_LONG).show();    

                        }
                        if("description".equalsIgnoreCase(event.getName())){
                             pe = parser.read();
                            if(pe.getText()!=null){
                                place.setdescription(pe.getText().toString());
                            }
                        }
                        traverse(parser); // recursion call for each <tag></tag>
                        break;
                    case Xml.END_TAG:
                        if ("Place".equalsIgnoreCase(event.getName())) {
                            places.addElement(place);
                         // Toast.makeText(MainPActivity.this, "8", Toast.LENGTH_LONG).show();  
                        }
                        leave = true;
                        break;
                    case Xml.END_DOCUMENT:
                        leave = true;
                        break;
                    case Xml.TEXT:
                        break;
                    case Xml.WHITESPACE:
                        break;
                    default:
                }
            } catch (Exception ex) {
                Toast.makeText(ac, ex.getMessage(), Toast.LENGTH_LONG).show();  

            }
        } while (!leave);

    }
}

共有1个答案

戈建白
2023-03-14

任何网络调用都应该在单独的线程中完成。您正在UI(默认)主线程中进行网络调用,而Android4.0+会抛出一个异常。查看AsyncTask或工作线程,以便在另一个进程中完成长期运行的任务

http://developer.android.com/guide/components/process-and-threads.html

 类似资料:
  • 本文向大家介绍C#使用LitJson解析JSON的示例代码,包括了C#使用LitJson解析JSON的示例代码的使用技巧和注意事项,需要的朋友参考一下 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立

  • 本文向大家介绍使用jquery解析XML示例代码,包括了使用jquery解析XML示例代码的使用技巧和注意事项,需要的朋友参考一下 xml文件结构:books.xml 页面代码: 效果图:

  • 本文向大家介绍PHP copy函数使用案例代码解析,包括了PHP copy函数使用案例代码解析的使用技巧和注意事项,需要的朋友参考一下 copy—拷贝文件 说明 copy(string$source,string$dest[,resource$context] ) :bool 将文件从source拷贝到dest。 如果要移动文件的话,请使用rename()函数。 参数 source 源文件路径。

  • 问题内容: 我试图在春季连接一个messageSource以用于我的应用程序。它不起作用,给出此错误: org.springframework.context.NoSuchMessageException:在代码“ validation_required”下找不到语言环境“ en”的消息。 我的 applicationContext.xml 包含messageSource的以下定义: 我的邮件属性

  • 本文向大家介绍MyBatis使用动态表或列代码解析,包括了MyBatis使用动态表或列代码解析的使用技巧和注意事项,需要的朋友参考一下 有时候会不可避免使用动态表或者列进行业务处理。下面学习几种动态表/列的使用方式: 【1】使用预编译 即,默认值。 预编译,即首先会生成select number from ? where name=? and date=? 这样使用”?”作为占位符的语句,然后进行

  • 本文向大家介绍Python greenlet和gevent使用代码示例解析,包括了Python greenlet和gevent使用代码示例解析的使用技巧和注意事项,需要的朋友参考一下 greenlet示例 greenlet微线程,允许在线程中手动切换 示例1,线程切换 gr1和gr2是两个greenlet线程,使用gr1.switch(..)启动gr1,gr1执行test1,切换到gr2,gr2执