1

嗨,我使用 kxml jar 来解析来自 Web 服务的数据。我以前在其他项目中使用过..但现在我在新项目中使用相同的,但我收到错误,我无法对其进行故障排除..

我的 xml 文件将如下所示:

<rss version="2.0">

<tttt>gsdsj</tttt>
<dddd>urkjrnf</dddd>
<item>
<title>ghsdsjhdsjdb</title>
<description>fgdhshbsdb</description>
</item>

<item>
<title>ghsdsjhdsjdb</title>
<description>fgdhshbsdb</description>
</item>

<item>
<title>ghsdsjhdsjdb</title>
<description>fgdhshbsdb</description>
</item>

</rss>

我想得到那个头衔。

我的解析器文件是:

public class TaplistingParser 
{
    public TaplistingParser()
    {

    }

    public String getXmlFromUrl(String url)
    {
        String xml=null;
        DefaultHttpClient httpclient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(url);
        try 
        {
            HttpResponse httpresponse = httpclient.execute(httpPost);
            HttpEntity httpentity=httpresponse.getEntity();
            xml=EntityUtils.toString(httpentity);
        } 
        catch (ClientProtocolException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//      System.out.println("xml="+xml);
        return xml;
    }

    public Document getDomElement(String xml)
    {
        Document doc=null;
        DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
        dbf.setCoalescing(true);
        try 
        {
            DocumentBuilder db= dbf.newDocumentBuilder();

            InputSource is= new InputSource();
            is.setCharacterStream(new StringReader(xml));
            try {
                doc=db.parse(is);
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } 
        catch (ParserConfigurationException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
        return doc;

    }

     public final String getElementValue( Node elem ) {
         Node child;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
//                   if( child.getNodeType() == Node.TEXT_NODE  ){
                         return child.getNodeValue();
//                   }
                 }
             }
         }
         return "";
     }

     /**
      * Getting node value
      * @param Element node
      * @param key string
      * */
     public String getValue(Element item, String str) {     
            NodeList n = item.getElementsByTagName(str);        
            return this.getElementValue(n.item(0));
        }
}

我的活动代码:

public void getelement()
    {

       TaplistingParser parser = new TaplistingParser();
       String xml= parser.getXmlFromUrl(URL);
       Document doc=parser.getDomElement(xml);        

       NodeList nl=doc.getElementsByTagName("item");
       title = new String[nl.getLength()];      

       for(int i=0; i < nl.getLength(); i++ )
       {

        Element e = (Element) nl.item(i);
        title[i]=parser.getValue(e, "title");                       
       }   

    }

我的日志是:

12-11 15:58:42.962: W/System.err(551): org.xml.sax.SAXParseException: expected: /META read: HEAD (position:END_TAG </HEAD>@11:8 in java.io.StringReader@44c60e80) 
12-11 15:58:42.982: W/System.err(551):  at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:151)
12-11 15:58:42.992: W/System.err(551):  at parser.TaplistingParser.getDomElement(TaplistingParser.java:67)
12-11 15:58:42.992: W/System.err(551):  at your.com.font.FontActivity.getelement(FontActivity.java:42)
12-11 15:58:43.023: W/System.err(551):  at your.com.font.FontActivity.onCreate(FontActivity.java:25)
12-11 15:58:43.032: W/System.err(551):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-11 15:58:43.032: W/System.err(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-11 15:58:43.032: W/System.err(551):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-11 15:58:43.042: W/System.err(551):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-11 15:58:43.042: W/System.err(551):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-11 15:58:43.042: W/System.err(551):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 15:58:43.042: W/System.err(551):  at android.os.Looper.loop(Looper.java:123)
12-11 15:58:43.042: W/System.err(551):  at android.app.ActivityThread.main(ActivityThread.java:4363)
12-11 15:58:43.042: W/System.err(551):  at java.lang.reflect.Method.invokeNative(Native Method)
12-11 15:58:43.042: W/System.err(551):  at java.lang.reflect.Method.invoke(Method.java:521)
12-11 15:58:43.042: W/System.err(551):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-11 15:58:43.042: W/System.err(551):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-11 15:58:43.052: W/System.err(551):  at dalvik.system.NativeStart.main(Native Method)
12-11 15:58:43.052: D/AndroidRuntime(551): Shutting down VM
12-11 15:58:43.052: W/dalvikvm(551): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-11 15:58:43.062: E/AndroidRuntime(551): Uncaught handler: thread main exiting due to uncaught exception
12-11 15:58:43.072: E/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{your.com.font/your.com.font.FontActivity}: java.lang.NullPointerException
12-11 15:58:43.072: E/AndroidRuntime(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-11 15:58:43.072: E/AndroidRuntime(551):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-11 15:58:43.072: E/AndroidRuntime(551):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-11 15:58:43.072: E/AndroidRuntime(551):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-11 15:58:43.072: E/AndroidRuntime(551):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 15:58:43.072: E/AndroidRuntime(551):  at android.os.Looper.loop(Looper.java:123)
12-11 15:58:43.072: E/AndroidRuntime(551):  at android.app.ActivityThread.main(ActivityThread.java:4363)
12-11 15:58:43.072: E/AndroidRuntime(551):  at java.lang.reflect.Method.invokeNative(Native Method)
12-11 15:58:43.072: E/AndroidRuntime(551):  at java.lang.reflect.Method.invoke(Method.java:521)
12-11 15:58:43.072: E/AndroidRuntime(551):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-11 15:58:43.072: E/AndroidRuntime(551):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-11 15:58:43.072: E/AndroidRuntime(551):  at dalvik.system.NativeStart.main(Native Method)
12-11 15:58:43.072: E/AndroidRuntime(551): Caused by: java.lang.NullPointerException
12-11 15:58:43.072: E/AndroidRuntime(551):  at your.com.font.FontActivity.getelement(FontActivity.java:44)
12-11 15:58:43.072: E/AndroidRuntime(551):  at your.com.font.FontActivity.onCreate(FontActivity.java:25)
12-11 15:58:43.072: E/AndroidRuntime(551):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-11 15:58:43.072: E/AndroidRuntime(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-11 15:58:43.072: E/AndroidRuntime(551):  ... 11 more
12-11 15:58:43.102: I/dalvikvm(551): threadid=7: reacting to signal 3
12-11 15:58:43.202: I/dalvikvm(551): Wrote stack trace to '/data/anr/traces.txt'

提前致谢..

4

0 回答 0