Skocz do zawartości

Pobieranie pliku xml z kursami walut NBP


Recommended Posts

Napisano

Witam mam problem z pobieraniem kursów walut do tablicy. Chciałbym aby pobrane kursy średnie dla danych walut były zapisywane w tablicy aby można było je dalej łatwo wykorzystać do przeliczania. Stworzyłem przycisk który ma wykonywac aktualizację tych kursów, lecz na androidzie wyskakuje bład android.os.NetworkOnMainThreadException. Dodałem do kodu    <uses-permission android:name="android.permission.INTERNET" /> lecz nadal nie działa.

Oto kod dla aktualizacji:

public void onClick(View vv) {
        TextView kod_waluty[];
        TextView kurs_sredni[];

        try {

            URL url = new URL("http://www.nbp.pl/kursy/xml/LastA.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("pozycja");

            kod_waluty = new TextView[nodeList.getLength()];
            kurs_sredni = new TextView[nodeList.getLength()];

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

                Node node = nodeList.item(i);

                Element kodElmnt = (Element) node;
                NodeList kodList = kodElmnt.getElementsByTagName("kod_waluty");
                Element kodElement = (Element) kodList.item(0);
                kodList = kodElement.getChildNodes();
                kod_waluty[i].setText(((Node) kodList.item(0)).getNodeValue());

                Element kursElmnt = (Element) node;
                NodeList kursList = kursElmnt.getElementsByTagName("kurs_sredni");
                Element kursElement = (Element) kursList.item(0);
                kodList = kursElement.getChildNodes();
                kurs_sredni[i].setText(((Node) kursList.item(0)).getNodeValue());
            }

        } catch (Exception e) {
            onClick.setText("Blad: " + e);
        }
    }
Napisano

Po modyfikacji kodu błąd nie wyskakuje, ale za to pojawia się inny java.lang.NullPointerException

Dołączam kod:

 

public void onClick(View vv) {


        new Thread(new Runnable(){
            @Override
            public void run() {
                try {

                    TextView kod_waluty[];
                    TextView kurs_sredni[];
                    try {

                        URL url = new URL("http://www.nbp.pl/kursy/xml/LastA.xml");
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
                        Document doc = db.parse(new InputSource(url.openStream()));
                        doc.getDocumentElement().normalize();

                        NodeList nodeList = doc.getElementsByTagName("pozycja");

                        kod_waluty = new TextView[nodeList.getLength()];
                        kurs_sredni = new TextView[nodeList.getLength()];


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



                            Node node = nodeList.item(i);

                            Element kodElmnt = (Element) node;
                            NodeList kodList = kodElmnt.getElementsByTagName("kod_waluty");
                            Element kodElement = (Element) kodList.item(0);
                            kodList = kodElement.getChildNodes();
                            kod_waluty[i].setText(((Node) kodList.item(0)).getNodeValue());

                            Element kursElmnt = (Element) node;
                            NodeList kursList = kursElmnt.getElementsByTagName("kurs_sredni");
                            Element kursElement = (Element) kursList.item(0);
                            kodList = kursElement.getChildNodes();
                            kurs_sredni[i].setText(((Node) kursList.item(0)).getNodeValue());
                        }

                    } catch (Exception e) {
                        onClick.setText("Blad: " + e);
                    }



                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }).start();


    }

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gość
Odpowiedz...

×   Wkleiłeś zawartość bez formatowania.   Usuń formatowanie

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Utwórz nowe...