Question

I'm trying to make an application that displays news feed from a website so I get the input stream and parse it in document using SAX but it returns SAX exception that it is unable to determine type of coding of this Stream . I tried before that to put The website's stream manually in XML file and read the file and It worked but when streaming directly from Internet it throws that exception and this is my code :

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();
        }

    }
}}

Any Ideas ??

Was it helpful?

Solution

I recommend restructuring this code into at least two parts.

I would create a download function that is given a URL and downloads the bytes associated with that URL. This should open and close the connection, and just return either the bytes downloaded or an error indication.

I would use this download processing as a 'function call' to download your XML bytes. Then parse the bytes that are obtained feeding these direct into your parser. If the data is properly constructed XML, it will have a header indicating the encoding used, so you do not need to worry about that, the parser will cope.

Once you have this parsed, then use the download function again to download the bytes associated with any images you want.

Regarding the SAX processing, have you reviewed this question:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top