Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpURLConnection setConnectTimeout()无效_Java_Android - Fatal编程技术网

Java HttpURLConnection setConnectTimeout()无效

Java HttpURLConnection setConnectTimeout()无效,java,android,Java,Android,我正在使用HTTPUrlConnection连接到一个简单的RSS提要。它工作得很好。我想为连接添加一个超时,因为我不希望我的应用程序在出现连接错误或其他情况时挂起。这是我使用的代码,setConnectTimeout方法没有任何效果 HttpURLConnection http = (HttpURLConnection) mURL.openConnection(); http.setConnectTimeout(15000); //timeout after 1

我正在使用HTTPUrlConnection连接到一个简单的RSS提要。它工作得很好。我想为连接添加一个超时,因为我不希望我的应用程序在出现连接错误或其他情况时挂起。这是我使用的代码,setConnectTimeout方法没有任何效果

        HttpURLConnection http = (HttpURLConnection) mURL.openConnection();
        http.setConnectTimeout(15000); //timeout after 15 seconds
...

如果有帮助的话,我正在android上开发。

您可能会选择其中一个/两个:
1)不要从连接读取任何内容
2)不要捕获并正确处理异常

如前所述,使用与以下类似的逻辑:

int TIMEOUT_VALUE = 1000;
try {
    URL testUrl = new URL("http://google.com");
    StringBuilder answer = new StringBuilder(100000);

    long start = System.nanoTime();

    URLConnection testConnection = testUrl.openConnection();
    testConnection.setConnectTimeout(TIMEOUT_VALUE);
    testConnection.setReadTimeout(TIMEOUT_VALUE);
    BufferedReader in = new BufferedReader(new InputStreamReader(testConnection.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) {
        answer.append(inputLine);
        answer.append("\n");
    }
    in.close();

    long elapsed = System.nanoTime() - start;
    System.out.println("Elapsed (ms): " + elapsed / 1000000);
    System.out.println("Answer:");
    System.out.println(answer);
} catch (SocketTimeoutException e) {
    System.out.println("More than " + TIMEOUT_VALUE + " elapsed.");
}

您还应该尝试设置读取超时(
http.setReadTimeout()
)。通常,web服务器会很乐意接受您的连接,但实际响应请求时可能会很慢。

在打开连接之前,请尝试设置
连接超时。

我遇到了类似的问题,因为中途下载。例如,如果你在下载的时候关掉wifi,我的手机会继续说它正在下载,并且停留在相同的百分比

我找到了一个解决方案,使用一个TimerTask连接到一个名为DownloaderTask的异步任务。尝试:

class Timeout extends TimerTask {
    private DownloaderTask _task;

    public Timeout(DownloaderTask task) {
        _task = task;
    }

    @Override
    public void run() {
        Log.w(TAG,"Timed out while downloading.");
        _task.cancel(false);
    }
};
然后在实际下载循环中设置超时错误计时器:

                    _outFile.createNewFile();
                    FileOutputStream file = new FileOutputStream(_outFile);
                    out = new BufferedOutputStream(file);
                    byte[] data = new byte[1024];
                    int count;
                    _timer = new Timer();
                    // Read in chunks, much more efficient than byte by byte, lower cpu usage.
                    while((count = in.read(data, 0, 1024)) != -1 && !isCancelled()) { 
                        out.write(data,0,count);
                        downloaded+=count;
                        publishProgress((int) ((downloaded/ (float)contentLength)*100));
                        _timer.cancel();
                        _timer = new Timer();
                        _timer.schedule(new Timeout(this), 1000*20);
                    }
                    _timer.cancel();
                    out.flush();

如果它超时,并且在20秒内连1K都无法下载,它将取消,而不是看起来永远在下载。

我也面临着同样的问题。设置
connectionTimeout
readTimeout
似乎不会像预期的那样返回异常,但确实需要。我花了一些时间来检查
URLConnection()
方法并了解发生了什么。
setConnectTimeout
的文档中有一条警告

如果主机名解析为多个IP地址,此客户端将尝试每个IP地址。如果连接到这些地址失败,在连接尝试引发异常之前,将经过多次超时 这意味着,如果主机解析了10个IP,则实际超时时间将为“10*ReadTimeStart”

您可以在IP上检查主机名的错误,因为它是由以下原因引起的:
1.您已连接到wifi,但没有internet连接。
2.您已连接到GSM数据,但传输非常差

在这两种情况下,大约20秒后都会出现主机异常。在我看来,获得正确答案的最佳方法是:

public boolean isOnline() {
        final int TIMEOUT_MILLS = 3000;
        final boolean[] online = {false};
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            final long time = System.currentTimeMillis();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        URL url = new URL("http://www.google.com");
                        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                        urlc.setConnectTimeout(TIMEOUT_MILLS);
                        urlc.setReadTimeout(TIMEOUT_MILLS);
                        urlc.connect();
                        if (urlc.getResponseCode() == 200) {
                            online[0] = true;
                        }
                    } catch (IOException e) {
                        loger.add(Loger.ERROR, e.toString());
                    }
                }
            }).start();

            while (((System.currentTimeMillis() - time) <= TIMEOUT_MILLS)) {
                if ((System.currentTimeMillis() - time) >= TIMEOUT_MILLS) {
                    return online[0];
                }
            }
        }
        return online[0];
    }
public boolean isOnline(){
最终整数超时=3000;
最终布尔值[]在线={false};
ConnectivityManager cm=(ConnectivityManager)context.getSystemService(context.CONNECTIVITY_服务);
NetworkInfo netInfo=cm.getActiveNetworkInfo();
如果(netInfo!=null&&netInfo.isConnected()){
最终长时间=System.currentTimeMillis();
新线程(newrunnable()){
@凌驾
公开募捐{
试一试{
URL=新URL(“http://www.google.com");
HttpURLConnection urlc=(HttpURLConnection)url.openConnection();
setConnectTimeout(超时);
setReadTimeout(超时);
connect();
如果(urlc.getResponseCode()==200){
在线[0]=真;
}
}捕获(IOE异常){
添加(loger.ERROR,例如toString());
}
}
}).start();
while(((System.currentTimeMillis()-time)=超时){
在线返回[0];
}
}
}
在线返回[0];
}
记住-在异步任务或服务中使用它


它的简单解决方案是,您使用HttpUrlConnection启动新线程(请记住使用start()not run())。而在while循环中,您需要等待3秒钟才能得到结果。如果没有发生任何情况,则返回false。这样可以避免等待主机异常,并避免出现setConnectTimeout()无法工作的问题当您没有internet连接时。

零值表示无限超时,这意味着必须发生连接,通常默认为零:

connection.setConnectTimeout(0);
connection.setReadTimeout(0);

请参考两件事供您考虑。如果您不希望应用程序挂起,请将连接方法放在单独的线程中。其次,您说它“起作用”很好,你在做什么来模拟一个坏连接?@Otra我用progressdialog将它放在一个单独的线程中。基本上,如果连接良好,任务就会完成它的任务。但是如果连接不好,progressdialog会保持很长时间。为了模拟一个坏连接,我减少了超时时间。而不是给它15分钟秒,1秒。仅用于测试。还是错误?HttpURLConnection.setReadTimeout(毫秒);@Jonsow请共享网络连接的完整代码如何-构造函数是受保护的方法?如果连接到路由器,但internet连接已断开,则对我无效。
connection.setConnectTimeout(0);
connection.setReadTimeout(0);