问题
Hey I try to send a HttpPost to an special Device in a local network over wifi. If I try the same code on a simple Java application it will work, but If I try the same code on android I am not able to send the HttpPost.
String uri = "http://192.168.3.201:8080/remote/json-rpc/getPeripherals";
Log.i("uri", uri);
String requestBody = "{\"jsonrpc\":\"2.0\",\"method\":"
+ "\"configclient_homedevices/getPeripherals\",\"id\":"
+ "\"1349942076918\",\"params\":[]}";
Log.i("uri", uri);
String contentType = "application/json";
String userpassword = "foo:foo";
String encode = Base64.encodeToString(userpassword.getBytes(),
Base64.URL_SAFE | Base64.DEFAULT);
try {
URL url = null;
url = new URL(uri);
connection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Basic" + encode);
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Length",
"" + Integer.toString(requestBody.getBytes().length));
connection.setUseCaches(true);
connection.setDoInput(true);
DataOutputStream out = null;
out = new DataOutputStream(connection.getOutputStream());
out.write(requestBody.getBytes());
out.flush();
out.close();
} catch (IOException ioE) {
connection.disconnect();
ioE.printStackTrace();
}
// connection.connect();
try {
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
Log.i("Status", toString().valueOf(connection.getResponseCode()));
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
this.response = response.toString();
Log.i("Response", response.toString());
} else {
Log.i("Status", toString().valueOf(connection.getResponseCode()));
Log.i("Status-message", connection.getResponseMessage());
connection.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
this function is in a subclass which extends from AsyncTask. In the Manifest I enabled the Permission.INTERNET
<uses-permission android:name="android.permission.INTERNET" />
The result is:
10-23 14:52:51.590: W/System.err(5983): java.net.SocketException: No route to host
10-23 14:52:51.590: W/System.err(5983): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
10-23 14:52:51.590: W/System.err(5983): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:369)
10-23 14:52:51.590: W/System.err(5983): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
10-23 14:52:51.590: W/System.err(5983): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
10-23 14:52:51.590: W/System.err(5983): at java.net.Socket.connect(Socket.java:901)
10-23 14:52:51.590: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
10-23 14:52:51.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
10-23 14:52:51.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
10-23 14:52:51.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
10-23 14:52:51.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
10-23 14:52:51.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:250)
10-23 14:52:51.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:217)
10-23 14:52:51.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:624)
10-23 14:52:51.600: W/System.err(5983): at com.febro.myfoo.fooLoginActivitiy$MakeConnection.doInBackground(fooLoginActivitiy.java:339)
10-23 14:52:51.610: W/System.err(5983): at com.febro.myfoo.fooLoginActivitiy$MakeConnection.doInBackground(fooLoginActivitiy.java:1)
10-23 14:52:51.610: W/System.err(5983): at android.os.AsyncTask$2.call(AsyncTask.java:252)
10-23 14:52:51.610: W/System.err(5983): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-23 14:52:51.610: W/System.err(5983): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-23 14:52:51.610: W/System.err(5983): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
10-23 14:52:51.610: W/System.err(5983): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
10-23 14:52:51.610: W/System.err(5983): at java.lang.Thread.run(Thread.java:1020)
10-23 14:52:54.600: W/System.err(5983): java.net.SocketException: No route to host
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
10-23 14:52:54.600: W/System.err(5983): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:369)
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
10-23 14:52:54.600: W/System.err(5983): at java.net.Socket.connect(Socket.java:901)
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
10-23 14:52:54.600: W/System.err(5983): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:736)
10-23 14:52:54.600: W/System.err(5983): at com.febro.myfoo.fooLoginActivitiy$MakeConnection.doInBackground(fooLoginActivitiy.java:351)
10-23 14:52:54.600: W/System.err(5983): at com.febro.myfoo.fooLoginActivitiy$MakeConnection.doInBackground(fooLoginActivitiy.java:1)
10-23 14:52:54.600: W/System.err(5983): at android.os.AsyncTask$2.call(AsyncTask.java:252)
10-23 14:52:54.600: W/System.err(5983): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-23 14:52:54.610: W/System.err(5983): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-23 14:52:54.610: W/System.err(5983): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
10-23 14:52:54.610: W/System.err(5983): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
10-23 14:52:54.610: W/System.err(5983): at java.lang.Thread.run(Thread.java:1020)
The Exception is thrown at
out = new DataOutputStream(connection.getOutputStream());
回答1:
This can happen when you connect your mobile via different network than your PC. Different networks can have different firewall rules.
PC is typically connected via internal network (typically Ethernet). While mobile phones are usually connected via WIFI with stronger firewall rules.
BTW as the error is: java.net.SocketException: No route to host
This has nothing to do with GET vs. POST. HTTP GET or POST is communicated to the server later after client is able to connect.
回答2:
I found a solution for my Issue. The Issue was a fixed IP adress in the url after changing this I received a Status Code 400 Bad Request. This issue I got before I switched the request to a async task subclass. The solution for the Bad Request I found in the wireshark protocol. The Client send 2 Hypertext transfer protocols in one http Post. This caused by the android.Base64.encode() methode. The encoder inserted a /n after encoding the user:password string. In this case the client sent one http with only basic auth and a second with the rest the body without the basic auth. So the server could not read the request.
To change this you should write android.Base64.encode(NO_WARP) this Flag will encode the username:password right.
来源:https://stackoverflow.com/questions/13031170/android-httpurlconnection-httppost-receive-a-socketexception-no-route-to-host