2

I was following a tutorial to create a weather app using a Weather Library.When i tried to display the temperature of a certain place i got an error.I think there must have been a problem due to updation of the library.I am going to share the code, video tutorial link and the error so that you can exactly point out the mistake.

CODE:

public class HomeGUI extends javax.swing.JFrame {

    public HomeGUI() {
        initComponents();
        getWeather();
    }
    private void getWeather() {
        WeatherDoc doc=new WeatherDoc("29226594","c");
        WeatherDisplay disp=new WeatherDisplay();
        System.out.println(disp.getTemperature());    
    }

    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }                       

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(HomeGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HomeGUI().setVisible(true);
            }
        });
    }

    ...
}

error:

run:
Mar 21, 2018 11:59:48 PM com.teknikindustries.yahooweather.WeatherDoc <init>
null
SEVERE: null
java.net.UnknownHostException: xml.weather.yahoo.com
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
    at sun.net.www.http.HttpClient.New(HttpClient.java:339)
    at sun.net.www.http.HttpClient.New(HttpClient.java:357)
    ...

BUILD SUCCESSFUL (total time: 10 seconds)

Any help would be appreciated.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • the reason is that service is no longer available under that url. – cool Mar 21 '18 at 18:45
  • In the video this program was successful in displaying the temperature but when I run the same program I get an error. – Calculus Programmer Mar 21 '18 at 18:45
  • as you can see that video is a quiet old one – cool Mar 21 '18 at 18:46
  • @cool so what should I do now.What measures can I take to fix it? – Calculus Programmer Mar 21 '18 at 18:46
  • you can check out this side.https://www.igorkromin.net/index.php/2016/03/27/yahoo-effectively-shut-down-its-weather-api-by-forcing-oauth-10-and-crippling-it/ – cool Mar 21 '18 at 18:47
  • @cool so the 9 minutes I invested in completing and learning this app is a complete waste ?I don't know much app development but I think this can be fixed.If I somehow use a newer library (one of the recent ones which people generally use) and change my getWeather() corresponding to that library,I think this might fix my problem. – Calculus Programmer Mar 21 '18 at 18:53
  • @cool thanks for sharing the notice but it doesn't help in fixing this problem right now.Maybe someone else may fix it by implementing a working library for my weather app. – Calculus Programmer Mar 21 '18 at 18:55

1 Answers1

2

In Java, the exceptions try to provide information about a specific problem. In your case:

java.net.UnknownHostException: xml.weather.yahoo.com

That exception should be obvious. The host name xml.weather.yahoo.com is not known. This is not a problem with any of the code but just that yahoo does not seem to publish that name anymore. If you look at the DNS results from this tool you can see:

DNS Record not found

You'll have to do some google-ing to find out if Yahoo still supports that weather protocol and if so, what the new hostname is for the protocol. This API page from yahoo came up for me for a search on yahoo weather xml.

<tldr> When a browser, or in your case a http client in code, goes to access a service by name (google.com, cnn.com, xml.weather.yahoo.com, etc.), the first thing that happens is a Domain Name Service (DNS) request to lookup the name so that the IP address can be found. It is that IP address then that is used by the browser or code to connect to the service and get results.

If the DNS does not return any information about the address then the browser typically returns something like a ERR_NAME_NOT_RESOLVED (chrome) error and your Java code throws something like an UnknownHostException. </tldr>

Gray
  • 115,027
  • 24
  • 293
  • 354
  • Can I use a different weather protocol from Google or some other company ?Am I allowed to do that?If so from where can I get it and learn about it's implementation.Can you provide me the link of a recent Weather app development video? – Calculus Programmer Mar 21 '18 at 19:00
  • Just added a API link from yahoo that might help @CalculusProgrammer. Just refresh my answer. – Gray Mar 21 '18 at 19:01
  • thanks for providing the link and explaining my problem.I now understand more deeply what my code does.I will surely try this API. – Calculus Programmer Mar 21 '18 at 19:06
  • I just used the latest API for the weather app V2.02 and removed the previous jar but upon doing so I am not getting the option for importing the packages from the jar on the getWeather() when I click on the error bulbs in the two statements mentioned within this method (I am using Netbeans). Previously I was getting that option. – Calculus Programmer Mar 22 '18 at 02:38
  • The new API I have used is yahoo-weather-java API 2.0.2 with all dependencies from group com.github.fedy2 – Calculus Programmer Mar 22 '18 at 02:48