java. net. Socketexception: network unreachable connection

I tried to download an 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");
}

This exception is sometimes thrown (I specify the 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.

Solution

You are facing a connection failure Does this happen on 3G, WiFi or "normal" connections on your computer?

In any case, you must assume that connections may be lost from time to time when writing your application For example, for mobile devices, this often happens in basements, basements, etc For PC applications, this happens less often, but sometimes

Retry may be a good solution A clean error message explaining the network cannot be provided at this time

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>