Android的java.net.SocketTimeoutException:连接超时 [英] Android java.net.SocketTimeoutException: Connection timed out

查看:867
本文介绍了Android的java.net.SocketTimeoutException:连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我遇到下面的错误在我的计划:

Recently I have encounter the following error in my program:

11-18 12:30:30.259: W/System.err(21368): java.net.SocketTimeoutException: Connection timed out
11-18 12:30:30.259: W/System.err(21368):    at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
11-18 12:30:30.259: W/System.err(21368):    at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
11-18 12:30:30.259: W/System.err(21368):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
11-18 12:30:30.259: W/System.err(21368):    at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
11-18 12:30:30.259: W/System.err(21368):    at java.net.Socket.connect(Socket.java:1002)
11-18 12:30:30.259: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
11-18 12:30:30.259: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
11-18 12:30:30.269: W/System.err(21368):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)
11-18 12:30:30.269: W/System.err(21368):    at com.example.simplevider.SimpleVideo$4.run(SimpleVideo.java:122)
11-18 12:30:30.279: W/System.err(21368):    at java.lang.Thread.run(Thread.java:1019)

这创造了这个错误的功能如下:

The function that created this error is as follows :

    private void sendStuff() {
    Log.e("sendStuff", "======================================");
    new Thread(new Runnable() {

        @Override
        public void run() {
            final int BUFFER_SIZE = 4096;
            try {
                File uploadFile = new File(existingFileName);
                System.out.println("File to upload: " + existingFileName);
                URL url = new URL(URL);

                HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
                httpConn.setUseCaches(false);
                httpConn.setDoOutput(true);
                httpConn.setRequestMethod("POST");
                httpConn.setReadTimeout(60*1000);
                httpConn.setConnectTimeout(60 * 1000);
                // sets file name as a HTTP header
                httpConn.setRequestProperty("fileName", uploadFile.getName());
                httpConn.setRequestProperty("extra-id", uploadFile.getAbsoluteFile().toString());
                httpConn.setRequestProperty("extra-id2", uploadFile.getParent());
                httpConn.setRequestProperty("extra-id3", uploadFile.length() + "");



                OutputStream outputStream = httpConn.getOutputStream(); //<< this is the source of the error


                FileInputStream inputStream = new FileInputStream(uploadFile);

                byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead = -1;

                System.out.println("Start writing data...");

                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }

                System.out.println("Data was written.");
                outputStream.close();
                inputStream.close();

                // always check HTTP response code from server
                int responseCode = httpConn.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    // reads server's response
                    BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
                    String response = reader.readLine();
                    System.out.println("Server's response: " + response);
                } else {
                    System.out.println("Server returned non-OK code: " + responseCode);
                }

            } catch (Exception e) {
                e.printStackTrace();
                sendStuff();
            }

        }
    }).start();

}

此功能是完全正常的JAVA工作,但复制时,它的Andr​​oid蒙上 java.net.SocketTimeoutException:连接超时,我不知道为什么

This function is perfectly working in normal JAVA but when copied to Android it casts java.net.SocketTimeoutException: Connection timed out and I don't know why.

推荐答案

有两种可能性,

1)你检查并测试您的连接。

1)have you checked and tested your connection.

2)最好不要设置任何连接超时,如果你设置中选择了最长时间,, BCOS它抛出一个错误,如果服务器没有在规定时间内没有响应。

2)better don't set any connection timeout,if you are setting chose maximum time,,bcos it throws an error,if server didn't response within given time..

这篇关于Android的java.net.SocketTimeoutException:连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆