0

我正在尝试使用以下代码在我的程序中解析 xml。但它在 logcat 中向我抛出了以下错误。任何人都可以请帮忙。

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.util.Log;







public class Secactivity
{
    public static String TAG = "Abeeee";
    public static String[] PodcastTitle;
    public static String[] PodcastURL;
    public static String[] PodcastContent;
    public static String[] PodcastMedia;
        public static void parse()
        {
                URL url;
                try
                {
                        // Set the url (you will need to change this to your RSS URL
                        url = new URL(
                                        "http://myurl");

                        // Setup the connection
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                        // Connect
                        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK)
                        {

                                // Retreive the XML from the URL
                                DocumentBuilderFactory dbf = DocumentBuilderFactory
                                                .newInstance();
                                DocumentBuilder db = dbf.newDocumentBuilder();
                                Document doc;
                                doc = db.parse(url.openStream());
                                doc.getDocumentElement().normalize();

                                // This is the root node of each section you want to parse
                                NodeList itemLst = doc.getElementsByTagName("employee");

                                // This sets up some arrays to hold the data parsed
                                PodcastTitle = new String[itemLst.getLength()];
                                PodcastURL = new String[itemLst.getLength()];
                                //PodcastContent = new String[itemLst.getLength()];
                                //PodcastMedia = new String[itemLst.getLength()];

                                // Loop through the XML passing the data to the arrays
                                for (int i = 0; i < itemLst.getLength(); i++)
                                {

                                        Node item = itemLst.item(i);
                                        if (item.getNodeType() == Node.ELEMENT_NODE)
                                        {
                                                Element ielem = (Element) item;

                                                // This section gets the elements from the XML
                                                // that we want to use you will need to add
                                                // and remove elements that you want / don't want
                                                NodeList title = ielem.getElementsByTagName("title");
                                                NodeList link = ielem.getElementsByTagName("link");
                                                //NodeList content = ielem
                                                //              .getElementsByTagName("content:encoded");
                                                //NodeList media = ielem
                                                    //            .getElementsByTagName("media:content");

                                                // This is an attribute of an element so I create
                                                // a string to make it easier to use
                                            // String mediaurl = media.item(0).getAttributes()
                                                            // .getNamedItem("url").getNodeValue();

                                                // This section adds an entry to the arrays with the
                                                // data retrieved from above. I have surrounded each
                                                // with try/catch just incase the element does not
                                                // exist
                                                try
                                                {
                                                        Log.i(TAG,"Parsing");
                                                        PodcastTitle[i] = title.item(0)
                                                                        .getChildNodes().item(0).getNodeValue();
                                                } catch (NullPointerException e)
                                                {
                                                        e.printStackTrace();
                                                }

                                                try
                                                {
                                                    PodcastURL[i] = link.item(0).getChildNodes()
                                                                        .item(0).getNodeValue();
                                                } catch (NullPointerException e)
                                                {
                                                        e.printStackTrace();
                                                }
                                            /* try
                                                {
                                                        PodcastContent[i] = content.item(0)
                                                                        .getChildNodes().item(0).getNodeValue();
                                                } catch (NullPointerException e)
                                                {
                                                        e.printStackTrace();
                                                }

                                                try
                                                {
                                                        PodcastMedia[i] = mediaurl;
                                                } catch (NullPointerException e)
                                                {
                                                        e.printStackTrace();
                                                }*/
                                        }
                                }
                        }

                } catch (MalformedURLException e)
                {
                        e.printStackTrace();
                } catch (DOMException e)
                {
                        e.printStackTrace();
                } catch (IOException e)
                {
                        e.printStackTrace();
                } catch (ParserConfigurationException e)
                {
                        e.printStackTrace();
                } catch (SAXException e)
                {
                        e.printStackTrace();
                }

        }

}

LOGCAT 条目:

01-10 05:20:25.964: E/ActivityThread(612): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d5f150 that was originally bound here
01-10 05:20:25.964: E/ActivityThread(612): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d5f150 that was originally bound here
01-10 05:20:25.964: E/ActivityThread(612):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-10 05:20:25.964: E/ActivityThread(612):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-10 05:20:25.964: E/ActivityThread(612):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-10 05:20:25.964: E/ActivityThread(612):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-10 05:20:25.964: E/ActivityThread(612):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-10 05:20:25.964: E/ActivityThread(612):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-10 05:20:25.964: E/ActivityThread(612):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-10 05:20:25.964: E/ActivityThread(612):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
01-10 05:20:25.964: E/ActivityThread(612):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
01-10 05:20:25.964: E/ActivityThread(612):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-10 05:20:25.964: E/ActivityThread(612):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-10 05:20:25.964: E/ActivityThread(612):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-10 05:20:25.964: E/ActivityThread(612):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-10 05:20:25.964: E/ActivityThread(612):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-10 05:20:25.964: E/ActivityThread(612):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-10 05:20:25.964: E/ActivityThread(612):  at java.lang.Thread.run(Thread.java:856)
01-10 05:20:26.105: E/StrictMode(612): null
01-10 05:20:26.105: E/StrictMode(612): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d5f150 that was originally bound here
01-10 05:20:26.105: E/StrictMode(612):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
01-10 05:20:26.105: E/StrictMode(612):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
01-10 05:20:26.105: E/StrictMode(612):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
01-10 05:20:26.105: E/StrictMode(612):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
01-10 05:20:26.105: E/StrictMode(612):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
01-10 05:20:26.105: E/StrictMode(612):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
01-10 05:20:26.105: E/StrictMode(612):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
01-10 05:20:26.105: E/StrictMode(612):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
01-10 05:20:26.105: E/StrictMode(612):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
01-10 05:20:26.105: E/StrictMode(612):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
01-10 05:20:26.105: E/StrictMode(612):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
01-10 05:20:26.105: E/StrictMode(612):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-10 05:20:26.105: E/StrictMode(612):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-10 05:20:26.105: E/StrictMode(612):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-10 05:20:26.105: E/StrictMode(612):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-10 05:20:26.105: E/StrictMode(612):  at java.lang.Thread.run(Thread.java:856)

主类:这是我调用 Secactivity 的地方:

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {



    EditText uid;
    EditText password;
    Button submit;

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        submit = (Button)findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                uid = (EditText)findViewById(R.id.id);
                password = (EditText)findViewById(R.id.password);
                String userid = uid.getText().toString();
                String pwd = password.getText().toString();
                Loginauth log = new Loginauth();
                if(!userid.equals("")&&!pwd.equals(""))
                {
                    String result = log.Login(userid,pwd);
                    if(result.equals(""))
                    {
                        Toast toast = Toast.makeText(getApplicationContext(), "Login Error: Check id and Password", Toast.LENGTH_LONG);
                        toast.show();
                    }
                    else
                    {
                        Toast toast = Toast.makeText(getApplicationContext(), "Login Successfull", Toast.LENGTH_SHORT);
                        toast.show();
                        //startActivity(new Intent("com.example.temp.ABC"));
                        Secactivity.parse();
                    }

                }
                else
                {
                    Toast toast = Toast.makeText(getApplicationContext(), "Invalid id or password", Toast.LENGTH_LONG);
                    toast.show();
                }

            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}
4

1 回答 1

1

试试这个链接,它解释了关于 xml 解析的一切

public class AndroidXMLParsingActivity extends ListActivity {
     static final String URL = "http://api.androidhive.info/pizza/?format=xml";
        // XML node keys
        static final String KEY_ITEM = "item"; // parent node
        static final String KEY_ID = "id";
        static final String KEY_NAME = "name";
        static final String KEY_COST = "cost";
        static final String KEY_DESC = "description";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_ITEM);
            // looping through all item nodes <item>
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
                map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
                map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

                // adding HashList to ArrayList
                menuItems.add(map);
            }

供参考.. http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/

http://developer.android.com/training/basics/network-ops/xml.html

于 2013-01-10T06:06:10.770 回答