0

我正在尝试制作一个显示来自网站的新闻提要的应用程序,因此我获取输入流并使用 SAX 在文档中解析它,但它返回 SAX 异常,它无法确定此 Stream 的编码类型。在此之前,我尝试将网站的流手动放入 XML 文件中并读取文件,它可以工作,但是当直接从 Internet 流式传输时,它会抛出该异常,这是我的代码:

public final class MyScreen extends MainScreen {
protected static RichTextField RTF = new RichTextField("Plz Wait . . . ",
        Field.FIELD_BOTTOM);
public MyScreen() {
    // Set the displayed title of the screen
    super(Manager.NO_VERTICAL_SCROLL);

    setTitle("Yalla Kora");
    Runnable R = new Runnable();
    R.start();
    add(RTF);

}

private class Runnable extends Thread {

    public Runnable() {
        // TODO Auto-generated constructor stub
        ConnectionFactory factory = new ConnectionFactory();
        ConnectionDescriptor descriptor = factory
                .getConnection("http://www.yallakora.com/arabic/rss.aspx?id=0");

        HttpConnection httpConnection;
        httpConnection = (HttpConnection) descriptor.getConnection();// Connector.open("http://www.yallakora.com/pictures/main//2011/11/El-Masry-807-11-2011-21-56-7.jpg");
        Manager mainManager = getMainManager();
        RichList RL = new RichList(mainManager, true, 2, 1);

        InputStream input;

        try {
            input = httpConnection.openInputStream();

            Document document;

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder docBuilder;
            try {
                docBuilder = docBuilderFactory.newDocumentBuilder();

                docBuilder.isValidating();
                try {
                    document = docBuilder.parse(input);

                    document.getDocumentElement().normalize();

                    NodeList item = document.getElementsByTagName("item");
                    int k = item.getLength();

                    for (int i = 0; i < k; i++) {

                        Node value = item.item(i);
                        NodeList Data = value.getChildNodes();

                        Node title = Data.item(0);
                        Node link = Data.item(1);
                        Node date = Data.item(2);
                        Node discription = Data.item(5);

                        Node Discription = discription.getFirstChild();

                        String s = Discription.getNodeValue();
                        int mm = s.indexOf("'><BR>");
                        int max = s.length();

                        String imagelink = s.substring(0, mm); 
                        String Khabar = s.substring(mm + 6, max);
                        String Date = date.getFirstChild().getNodeValue();
                        String Title = title.getFirstChild().getNodeValue();
                        String Link = link.getFirstChild().getNodeValue();

                        ConnectionFactory factory1 = new ConnectionFactory();
                        ConnectionDescriptor descriptor1 = factory1
                                .getConnection(imagelink);

                        HttpConnection httpConnection1;
                        httpConnection1 = (HttpConnection) descriptor1
                                .getConnection();
                        InputStream input1;

                        input1 = httpConnection1.openInputStream();
                        byte[] bytes = IOUtilities.streamToBytes(input1);
                        Bitmap bitmap = Bitmap.createBitmapFromBytes(bytes,
                                0, -1, 1);

                        ;
                        RL.add(new Object[] { bitmap, Title, Khabar, Date });
                        add(new RichTextField(link.getNodeValue(),
                                Field.NON_FOCUSABLE));

                    }

                    RTF.setText("");
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    RTF.setText("SAXException " + e.toString());
                    e.printStackTrace();
                }
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                RTF.setText("ParserConfigurationException " + e.toString());
                e.printStackTrace();
            }
        } catch (IOException e) {
            RTF.setText("IOException " + e.toString());
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}}

有任何想法吗 ??

4

1 回答 1

0

我建议将此代码重组为至少两部分。

我将创建一个下载函数,该函数给出一个 URL 并下载与该 URL 关联的字节。这应该打开和关闭连接,并且只返回下载的字节或错误指示。

我会将此下载处理用作“函数调用”来下载您的 XML 字节。然后解析获得的字节,将这些字节直接输入到您的解析器中。如果数据是正确构造的 XML,它将有一个标头指示使用的编码,因此您无需担心,解析器会处理。

解析完成后,再次使用下载功能下载与您想要的任何图像相关的字节。

关于 SAX 处理,您是否查看过这个问题:

parse-xml-inputstream-in-blackberry-java-application

于 2013-11-01T18:31:14.400 回答