“no route to host” on device on getOutputStream()

2020-05-07 19:35发布

问题:

private String urlPost = "http://192.168.1.66:8080/DataCollectionServlet/";

@Override
protected void doWakefulWork(Intent intent) {
    // https://stackoverflow.com/q/14630255/281545
    HttpURLConnection connection = null;
    try {
        connection = connection();
        w("connection"); // allrigt
        final OutputStream connOutStream = connection.getOutputStream();
        w("GEToUTPUTsTREAM"); // I never see this
    } catch (IOException e) {
        e.printStackTrace(); // No route to host
    } finally {
        if (connection != null) connection.disconnect();
    }
}

private HttpURLConnection connection() throws MalformedURLException,
        IOException {
    HttpURLConnection connection = (HttpURLConnection) new URL(urlPost)
            .openConnection();
    // connection.setDoInput(true);
    connection.setDoOutput(true); // triggers POST
    // connection.setUseCaches(false); // needed ?
    // TODO : http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
    // connection.setRequestProperty("Connection", "Keep-Alive");
    // connection.setRequestProperty("User-Agent",
    // "Android Multipart HTTP Client 1.1"); // needed ?
    return connection;
}

The server in 192.168.1.66:8080/DataCollectionServlet/ is up and running. My device IP is 192.168.1.65. I disabled both window's and the router's firewall to no avail.

EDIT - stack trace :

java.net.SocketException: No route to host
at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
at java.net.Socket.connect(Socket.java:983)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)
at gr.uoa.di.monitoring.android.services.NetworkService.doWakefulWork(NetworkService.java:51)
at com.commonsware.cwac.wakeful.WakefulIntentService.onHandleIntent(WakefulIntentService.java:94)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.os.HandlerThread.run(HandlerThread.java:60)

Comments : Again, the phone and PC are both connected to the same router wirelessly (same SSID)

Android 2.3.7 Eclair, HTC Nexus 1 - could it be a bug ?

EDIT 2013.11.13 : still interested in ana explanation of my answer

回答1:

Well well - disabling the (WPA-PSK) Encryption on the router I connect alright. I would accept as an answer a (programmatic) work around

EDIT : entering the URL into the android device's browser I do see the doGet() page. I reenabled all firewalls - the only thing that makes a difference is toggling encryption on and off