How to reiterate the function automatically if my code stops working because my internet disconnected

1 view (last 30 days)
Hello,
I want to get weather data from a URL given below:
xdom = xmlread('http://api.openweathermap.org/data/2.5/forecast? q=Texel,NL&mode=xml&APPID=f3e1e8697d3d20e5d91f193ef02e4846')
Now my whole code is running properly, but due to bad internet connection my code stopped. To safeguard my whole code that it doesn't happen again, I want to develop a code which will help in reiterating the code after every 30 seconds until the internet connection is restored. Can someone help me with the same.

Accepted Answer

Jan
Jan on 14 Apr 2015
How does this code stop? With an error message? Then TRY-CATCH is a direct solution:
success = false;
while ~success
try
xdom = xmlread('...');
success = true;
catch ME
disp('Failed:');
disp(ME.message);
pause(30);
end
end
  1 Comment
Punit Gandhi
Punit Gandhi on 15 Apr 2015
Hey Jan,
Thanks for the code. It works perfectly. It shows me this error when internet connection is not working.
>> isnet Failed: Java exception occurred: java.net.UnknownHostException: api.openweathermap.org
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!