11

I am trying to download a xml text file from a web server using this method:

static void download (String url , String fileName) throws IOException{

            FileWriter xmlWriter;
            xmlWriter = new FileWriter(fileName);
            System.out.println("URL to download is : " + url);

            // here Exception is thrown/////////////////////////////////
            BufferedReader inputTxtReader = new BufferedReader
                        (new BufferedReader(new InputStreamReader(addURL.openStream())));
            ////////////////////////////////////////////////////////

            String str ;
            String fileInStr = "";

            str = inputTxtReader.readLine();

            while (!(str == null)  ){///&& !(str.equals("</tv>"))
                fileInStr += (str + "\r\n");
                str = inputTxtReader.readLine();
            }

            xmlWriter.write(fileInStr);
            xmlWriter.flush();
            xmlWriter.close();
            System.out.println("File Downloaded");
}

Sometimes this exception is thrown (where I specified is code):

java.net.SocketException: Network is unreachable: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.Socket.connect(Socket.java:518)
    at java.net.Socket.connect(Socket.java:468)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:389)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:516)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:318)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:788)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:729)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:654)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:977)
    at java.net.URL.openStream(URL.java:1009)
    at MessagePanel.download(MessagePanel.java:640)
    at WelcomThread.run(MainBody2.java:891)

Please guide me

Thank you all.

2
  • 1
    !(str == null) just looks confusing, you should write str != null. Commented Jul 26, 2011 at 7:08
  • 1
    Where and how are you initializing the addURL variable? Commented Jul 26, 2011 at 7:16

6 Answers 6

14

You are facing a connection breakdown. Does this happen in 3G, WiFi or "plain" connection on a computer?

Anyway, you must assume that the connection may be lost from time to time, when writing your app. For example, with mobiles, this happens frequently in the tube, in basements, etc. With PC apps, this is less frequent but occurs sometimes.

A retry can be a good solution. And a clean error message that explains the network is not available at this moment too.

Sign up to request clarification or add additional context in comments.

1 Comment

I added a thread that as request time outs it tries again. this this was effective. because in my network we broadcast several media contents; this problems occur. thank you for help
2

I faced situation of getting java.net.SocketException not sometimes but every time. I've added -Djava.net.preferIPv4Stack=true to java command line and my program started to work properly.

Comments

1

"Network is unreachable" means just that. You're not connected to a network. It's something outside of your program. Could be a bad OS setting, NIC, router, etc.

1 Comment

That too, but is it me or is there no definition for addURL anywhere in there?
0

I haven't tested with your code so it would be totally different case though, still I'd like to share my experience. (Also this must be too late answer though, I hope this answer still would help somebody in the future)

I recently faced similar experience like you such as some times Network is unreachable, but sometimes not. In short words, what was cause is too small time out. It seems Java throws IOException with stating "Network is unreachable" when the connection fails because of it. It was so misleading (I would expect something like saying "time out") and I spent almost a month to detect it.

Here I found another post about how to set time out. Alternative to java.net.URL for custom timeout setting

Again, this might not the same case as you got experienced, but somebody for the future.

1 Comment

`'Network unreachable' provides a lot more informative than 'time out', and in fact the two are not related.
0

this just happened to me. None of the answers helped, as the issue was I have recently changed the target host configuration and put incorrect host value there. So it could just be wrong connection details as well.

Comments

-1

In my case, my internet connection itself had DNS that apparently couldn't resolve the hostname.

3 Comments

That would cause NoSuchHostException, not 'network unreachable'.
It did give me java.net.SocketException using okhttp and kotlin.
I've tried recreating the same exception by reverting the DNS back back to cloudflare, and now it somehow works (even though the last time it was completely resolved right after i changed it to Google). mapping the original host to a loopback address resulted in SocketTimeoutException: Network is unreachable. Having a completely non-existent domain as the url did result in UnknownHostException. Those are my findings. Should I delete the response?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.