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.
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>