嘿,我尝试通过 wifi 将 HttpPost 发送到本地网络中的特殊设备。如果我在一个简单的 Java 应用程序上尝试相同的代码,它将起作用,但如果我在 android 上尝试相同的代码,我将无法发送 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();
}
此函数位于从 AsyncTask 扩展的子类中。在清单中我启用了 Permission.INTERNET
<uses-permission android:name="android.permission.INTERNET" />
结果是:
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)
异常被抛出
out = new DataOutputStream(connection.getOutputStream());