1

嘿,我尝试通过 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());
4

2 回答 2

3

当您通过与 PC 不同的网络连接手机时,可能会发生这种情况。不同的网络可以有不同的防火墙规则。

PC 通常通过内部网络(通常是以太网)连接。而手机通常通过防火墙规则更强的WIFI连接。

顺便说一句,错误是:java.net.SocketException: No route to host这与 GET 与 POST 无关。HTTP GET 或 POST 稍后在客户端能够连接后与服务器通信。

于 2012-10-23T14:05:48.317 回答
1

我为我的问题找到了解决方案。更改此问题后,问题是 url 中的固定 IP 地址,我收到了Status Code 400 Bad Request。我在将请求切换到异步任务子类之前遇到了这个问题。我在wireshark协议中找到的Bad Request的解决方案。客户端在一个 http Post 中发送 2 个超文本传输​​协议。这是由android.Base64.encode()方法引起的。编码器在对 user:password 字符串进行编码后插入了 /n。在这种情况下,客户端发送了一个只有基本身份验证的 http,第二个是没有基本身份验证的正文。所以服务器无法读取请求。

要更改这一点,您应该编写android.Base64.encode(NO_WARP)此标志将对用户名:密码进行正确编码。

于 2012-10-26T11:39:43.647 回答