将数据从一个设备传输到另一个设备 - android

时间:2018-01-25 06:10:32

标签: android android-wifi wifi-direct wifip2p socket-timeout-exception

所以我创建了两个独立的应用程序,一个充当客户端和另一个充当服务器。两个设备都使用热点连接:

设备之间的连接成功。为了将数据从服务器传输到客户端,我使用以下代码:

    public static String downloadDataFromSender(String apiUrl) throws IOException {
    InputStream is = null;
    try {
        URL url = new URL(apiUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);

        /* Starts the query  */
        conn.connect();
        conn.getResponseCode();

        is = conn.getInputStream();

        /* Convert the InputStream into a string  */
        return readIt(is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

但我一直收到以下错误:

java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:343)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:205)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:187)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
at java.net.Socket.connect(Socket.java:586)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:113)
at com.android.okhttp.Connection.connectSocket(Connection.java:196)
at com.android.okhttp.Connection.connect(Connection.java:172)
at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
at com.wifiscanner.utils.DownloadUtils.downloadDataFromSender(DownloadUtils.java:27)
at com.wifiscanner.tasks.SenderAPITask.doInBackground(SenderAPITask.java:29)
at com.wifiscanner.tasks.SenderAPITask.doInBackground(SenderAPITask.java:14)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)

我在下载中使用的网址是:http://192.168.43.1:52287/files

正如我所说,连接成功。所以不确定为什么我的网络无法访问。这种情况间歇性发生,并非总是如此。

有人可以提供一些有关为何发生这种情况的细节吗?

0 个答案:

没有答案