Xml Parser意外的令牌错误位置:java.io.StringReader中的TEXT @ 1:2

时间:2016-04-20 04:54:33

标签: java android xml xml-parsing domparser

问题是当我从原始链接解析XML时,例如(由于安全目的,我没有发布原始链接) http://example.com/ss.svc/APIabc?A=10&Key=XXXXX&From=19&To=221&Date=19-Apr-2016然后没有填充listview并且logcat说错误

LogCat错误 -

 Unexpected token (position:TEXT @1:2 in java.io.StringReader@4255df00) 
 Shutting down VM
 threadid=1: thread exiting with uncaught exception (group=0x41b19438)
 FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.CustomizedListView}: java.lang.NullPointerException
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
 at android.app.ActivityThread.access$700(ActivityThread.java:143)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:4960)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:511)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
 at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
 at com.example.androidhive.CustomizedListView.onCreate(CustomizedListView.java:48)
 at android.app.Activity.performCreate(Activity.java:5203)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
 ... 11 more

但是当相同的XML被放到另一个服务器链接时,然后链接以.php结尾,例如这个 http://www.example.in/exapmle/sub.php t ,它将解析后的数据显示给listview。

说我的XML格式是这样的

<APIabc
    xmlns="http://exapmle.org/">
    <APIabcd
        xmlns:a="http://ac.ssc.org/2014/21/abfaggkk"
        xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Route>
            <Time>2016-04-13 06:11:00 AM</Time>
            <Avail>xyz</Avail>
            <thumb_url>http://api.androidhive.info/music/images/adele.png</thumb_url>
            <BusLabel/>
        </Route>
    </APIabcd>
</APIabc>

CustomizedListView.java

public class CustomizedListView extends Activity {
        // All static variables
        static final String URL = "http://example.com/ss.svc/APIabc?A=10&Key=XXXXX&From=19&To=221&Date=19-Apr-2016";
        // XML node keys 
        static final String KEY_SONG = "Route"; // parent node
    static final String KEY_ID = "Avail";
    static final String KEY_TITLE = "Avail";
    static final String KEY_ARTIST = "Avail";
    static final String KEY_ARTIST2 = "Avail";
    static final String KEY_DURATION = "Avail";
    static final String KEY_THUMB_URL = "thumb_url";

        ListView list;
        LazyAdapter adapter;

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

            ArrayList&lt;HashMap&lt;String, String&gt;&gt; songsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();

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

            NodeList nl = doc.getElementsByTagName(KEY_SONG);
            // looping through all song nodes &lt;song&gt;
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key =&gt; value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

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

            list=(ListView)findViewById(R.id.list);

            // Getting adapter by passing xml data ArrayList
            adapter=new LazyAdapter(this, songsList);
            list.setAdapter(adapter);

            // Click event for single list row
            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                        int position, long id) {

                }
            });
        }
    }

由于logcat说错误com.example.androidhive.CustomizedListView.onCreate(CustomizedListView.java:48)所以代码中的第48行

NodeList nl = doc.getElementsByTagName(KEY_SONG);

1 个答案:

答案 0 :(得分:0)

我的自我解决了。我解决它的方式就像 - 我在代码

中注释掉了以下几行
   // InputSource is = new InputSource();
   // is.setCharacterStream(new StringReader(xml));
    doc = db.parse(is); 

并将网址直接传递给此行

 doc = db.parse("http://yourURL.xml");