J
J
javaProger982019-06-17 22:02:02
Java
javaProger98, 2019-06-17 22:02:02

How to make an android app exchange rate?

It is necessary to take the exchange rate data from the Central Bank, there is a link, an xml file is issued on it. If I do it in IntelijIdea, then everything works, it is displayed.

spoiler
import org.w3c.dom.*;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class Main {

    public static void main(String[] args){

        try {
            String pre_apiURL = "https://www.cbr-xml-daily.ru/daily_utf8.xml";
            System.out.println("url " + pre_apiURL);
            URL url = new URL(pre_apiURL);

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(url.openStream());
            NodeList list = doc.getElementsByTagName("Valute");
            Element element = doc.getDocumentElement();

            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element el = (Element) node;
                    String name1 = el.getAttribute("Name");

                    NodeList nodelist = el.getChildNodes();
                    for (int j = 0; j < nodelist.getLength(); j++) {
                        Node n = nodelist.item(j);
                        if (n.getNodeType() == Node.ELEMENT_NODE) {
                            Element name = (Element) n;
                            if (name.getTagName().equals("Name")) {
                                System.out.println(name.getTextContent());
                            }
                            if (name.getTagName().equals("Value")) {
                                System.out.println(name.getTextContent());
                            }
                        }

                    }
                }
            }



            }catch(Exception e){
            }
        }

}


Попробовала переделать под андроид и ничего не выводит объясните в чем ошибка и как исправить?
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import android.widget.TextView;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;



public class MainActivity extends AppCompatActivity {

   private ListView name;
   private ListView value;

    public String Name[];
    public  String Value[];

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

        ListView lvMain = (ListView) findViewById(R.id.name);
        try {
            String pre_apiURL = "https://www.cbr-xml-daily.ru/daily_utf8.xml";
            System.out.println("url " + pre_apiURL);
            URL url = new URL(pre_apiURL);

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(url.openStream());
            NodeList list = doc.getElementsByTagName("Valute");
             Element element = doc.getDocumentElement();

            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element el = (Element) node;
                      String name1 = el.getAttribute("Name");

                    NodeList nodelist = el.getChildNodes();
                    for (int j = 0; j < nodelist.getLength(); j++) {
                        Node n = nodelist.item(j);
                        if (n.getNodeType() == Node.ELEMENT_NODE) {
                            Element name = (Element) n;
                            if (name.getTagName().equals("Name")) {
                                Name[j]=name.getTextContent()+".";
                                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                                        android.R.layout.simple_list_item_1, Name);
                                lvMain.setAdapter(adapter);

                            }
                            if (name.getTagName().equals("Value")) {
                                Value[j]=name.getTextContent();
                            }
                        }

                    }
                }
            }



        }catch(Exception e){
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2019-06-17
@iLLuzor

"outputs nothing" is not a correct description of the problem, but a mockery. It is customary to tell what exactly is going wrong.
You can immediately see that you are accessing the network from the ui thread. The request to the network must be made in a separate thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question