解析XML文件时出现非法参数异常

时间:2013-03-06 15:13:00

标签: android xml illegalargumentexception xmlpullparser

我正在尝试拉出可以在这里看到的XML文件:

http://feeds.pcworld.com/pcworld/latestnews

这是我的MainActivity

    public class MainActivity extends ListActivity {

    ArrayAdapter<Item> adapter;
    List<Item>items;//Holds item objects containing info relating to element pulled from XML file.
    Item item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //initialize variables
        items = new ArrayList<Item>();

        new PostTask().execute();

        adapter=  new ArrayAdapter<Item>(this, android.R.layout.simple_list_item_1, items);
        setListAdapter(adapter);        

    }

    private InputStream getInputStream(URL url) {
        try{
            return url.openConnection().getInputStream();
        }catch(IOException e){
            return null;
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Uri uri = items.get(position).getLink();
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    @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;
    }

    //ASYNC CLASS
    private class PostTask extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... arg0) {
            try{
                //link to data source
                URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");

                //Set up parser
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(false);
                XmlPullParser xpp = factory.newPullParser();

                //get XML from input stream
                xpp.setInput(getInputStream(url), "UTF_8");

                //Keep track of which tag inside of XML
                boolean insideItem = false;

                //Loop through the XML file and extract data required
                int eventType = xpp.getEventType();

                while (eventType != XmlPullParser.END_DOCUMENT) {

                    if (eventType == XmlPullParser.START_TAG) {
                        Log.v("ENTER", String.valueOf(xpp.getEventType()));

                        if (xpp.getName().equalsIgnoreCase("item")) {
                            insideItem = true;

                            //Create new item object
                            item = new Item();

                        } else if (xpp.getName().equalsIgnoreCase("title")) {
                            if (insideItem){
                                item.setTitle(xpp.nextText());
                            }

                        } 

                        else if (xpp.getName().equalsIgnoreCase("description")) {
                            if (insideItem){
                                item.setDescription(xpp.nextText());
                            }
                        }

                        else if (xpp.getName().equalsIgnoreCase("link")) {
                            if (insideItem){
                                item.setLink(Uri.parse(xpp.nextText()));                            
                            }
                        }
                    }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){

                        insideItem=false;
                        //add item to list
                        items.add(item);

                    }


                    eventType = xpp.next(); //move to next element
                    publishProgress();
                }


                    } catch (MalformedURLException e) {

                        e.printStackTrace();

                    } catch (XmlPullParserException e) {

                        e.printStackTrace();

                    } catch (IOException e) {

                        e.printStackTrace();

                    }


            return "COMPETED";
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            adapter.notifyDataSetChanged();

        }

        public void onPostExecute(String s) {
            Toast.makeText(getApplicationContext(), s + " Items: " + items.size(), Toast.LENGTH_SHORT).show();
            adapter.notifyDataSetChanged();
        }

    }

}

这是Item

    public class Item {

    //Variables
    private String title;
    private Uri link;
    private String description;

    public Item() {
        super();
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Uri getLink() {
        return link;
    }
    public void setLink(Uri link) {
        this.link = link;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

}

但是,当我运行我的应用程序时,我收到此错误

由org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1615)中的java.lang.IllegalArgumentException引起 at com.example.simplerss.MainActivity $ PostTask.doInBackground(MainActivity.java:86) at com.example.simplerss.MainActivity $ PostTask.doInBackground(MainActivity.java:1) 在android.os.AsyncTask $ 2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305)

这是堆栈跟踪:

03-06 15:06:08.095: E/AndroidRuntime(670): FATAL EXCEPTION: AsyncTask #1
03-06 15:06:08.095: E/AndroidRuntime(670): java.lang.RuntimeException: An error occured while executing doInBackground()
03-06 15:06:08.095: E/AndroidRuntime(670):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-06 15:06:08.095: E/AndroidRuntime(670):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-06 15:06:08.095: E/AndroidRuntime(670):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-06 15:06:08.095: E/AndroidRuntime(670):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-06 15:06:08.095: E/AndroidRuntime(670):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-06 15:06:08.095: E/AndroidRuntime(670):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-06 15:06:08.095: E/AndroidRuntime(670):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-06 15:06:08.095: E/AndroidRuntime(670):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-06 15:06:08.095: E/AndroidRuntime(670):  at java.lang.Thread.run(Thread.java:856)
03-06 15:06:08.095: E/AndroidRuntime(670): Caused by: java.lang.IllegalArgumentException
03-06 15:06:08.095: E/AndroidRuntime(670):  at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1615)
03-06 15:06:08.095: E/AndroidRuntime(670):  at com.example.simplerss.MainActivity$PostTask.doInBackground(MainActivity.java:86)
03-06 15:06:08.095: E/AndroidRuntime(670):  at com.example.simplerss.MainActivity$PostTask.doInBackground(MainActivity.java:1)
03-06 15:06:08.095: E/AndroidRuntime(670):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-06 15:06:08.095: E/AndroidRuntime(670):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-06 15:06:08.095: E/AndroidRuntime(670):  ... 5 more

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

我刚检查了KXmlParser source code。当IllegalArgumentException传递给InputStream方法为setInput时,它会抛出null。这意味着getInputStream方法有问题,它返回null。确保您传递了有效的URL值并将android.permission.INTERNET添加到AndroidManifest

还尝试更改getInputStream方法:

private InputStream getInputStream(URL url) throws IOException {
    return url.openConnection().getInputStream();
}

答案 1 :(得分:0)

经过多次检查后发现我的校园防火墙出了问题。我已经设法从我的家用电脑运行应用程序,错误不存在。谢谢你的帮助。