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;
}
192.168.1.66:8080/DataCollectionServlet/
中的服务器已启动并正在运行。我的device IP是192.168.1.65
。我禁用了窗口和路由器的防火墙都无济于事。
编辑 - 堆栈跟踪:
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)
评论:同样,手机和PC都无线连接到同一个路由器(相同的SSID)
Android 2.3.7 Eclair,HTC Nexus 1 - 这可能是个错误吗?
编辑2013.11.13:仍然对my answer
的解释感兴趣答案 0 :(得分:0)
很好 - 在我连接的路由器上禁用(WPA-PSK
)加密。我会接受一个(程序化的)工作的答案
编辑:在Android设备的浏览器中输入URL我看到了doGet()
页面。我重新启用了所有防火墙 - 唯一有所作为的是打开和关闭加密