0

Java 编程

我有一个类 WeatherAgent。这个类的作用是从World weather online的一个URL(XML代码)中获取当前温度值和其他参数。

我的主要问题是;如何将 WeatherAgent 类中生成的温度值转换为另一个类(锅炉)?因此,将 WeatherAgent 类中的 XML 值作为另一个类(锅炉类)中的新变量访问。

package assignment_4;


    /*
     * imports of class WeatherAgent2
     */
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;

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

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


    /*
     *start class WeatherAgent2  
     */

    public class WeatherAgent {

        private Document getDocument() {

            Document doc = null;

            try {

                URL url = new URL(
                        "http://api.worldweatheronline.com/free/v1/weather.ashx?q=Eindhoven&format=xml&num_of_days=5&key=87xdd77n893f6akfs6x3jk9s");

                URLConnection connection = url.openConnection(); // Connecting to
                                                                    // URL specified

                doc = parseXML(connection.getInputStream());

            } catch (Exception e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            return doc;

        }

        private Document parseXML(InputStream stream) throws Exception

        {

            DocumentBuilderFactory objDocumentBuilderFactory = null;

            DocumentBuilder objDocumentBuilder = null;

            Document doc = null;

            try

            {

                objDocumentBuilderFactory = DocumentBuilderFactory.newInstance();

                objDocumentBuilder = objDocumentBuilderFactory.newDocumentBuilder();

                doc = objDocumentBuilder.parse(stream);
            }

            catch (Exception ex)

            {

                throw ex;

            }

            return doc;

        }



        /*
         * get the temperature
         */
        public double temperature() {

            Document doc = getDocument();

            Node Temperature;

            Double temp = 0.0;

            NodeList forecast = doc.getElementsByTagName("temp_C");

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

                Element element = (Element) forecast.item(i);

                NodeList list1 = (NodeList) element.getChildNodes();

                Temperature = list1.item(0);

                temp = Double.parseDouble(Temperature.getNodeValue());

            }

            return temp;

        }

        /*
         * get cloud cover
         */
        public double cloudcover() {

            Document doc = getDocument();

            Node Cloudcover;

            Double cloudcover = 0.0;

            NodeList forecast = doc.getElementsByTagName("cloudcover");

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

                Element element = (Element) forecast.item(i);

                NodeList list2 = (NodeList) element.getChildNodes();

                Cloudcover = list2.item(0);

                cloudcover = Double.parseDouble(Cloudcover.getNodeValue());

            }

            return cloudcover;

        }





        /*
         * print method
         */
        public static void main(String[] argvs) {

            WeatherAgent wp = new WeatherAgent();

            System.out.println("Temperature: " + wp.temperature());

            System.out.println("Cloudcover: " + wp.cloudcover());

        }

    }

我在锅炉类中有这个方法,可以将 WeatherAgent 类中的温度值获取到锅炉类中。它不工作,我知道为什么。已经感谢您的帮助!

public double getValueFromAgent(){


         double agentTemp = send(URL.create("http://api.worldweatheronline.com/free/v1/weather.ashx?q=Eindhoven&format=xml&num_of_days=5&key=87xdd77n893f6akfs6x3jk9s", "temperature" , null, Double.class));

                 return agentTemp;

     }
4

1 回答 1

1

有一些设计模式可以帮助您构建类以共享这样的信息。我会研究观察者模式和中介者模式。

或者主要是,当您从 WeatherAgent 获取温度时,将其推入锅炉。有了所有这些,我们的想法是让 Boiler 不知道任何关于 WeatherAgent 的事情。它有助于可重用性,并使独立测试它们变得容易得多。

于 2014-06-08T19:19:56.917 回答