首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InputStream超时

InputStream超时
EN

Stack Overflow用户
提问于 2012-10-16 20:14:54
回答 2查看 1.4K关注 0票数 0

有没有办法给InputStream设置超时时间?

我当前的代码是这样的:

代码语言:javascript
复制
public class DownloadFile implements Runnable {
private EventHandler eh;
private String source;
private String destination;

public void run(){
    try {
        Log.d("Download", "Download... " + source);
        URL url = new URL(source);

        URLConnection connection = url.openConnection();
        connection.connect();

        int fileLength = connection.getContentLength();

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(destination + "t");
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-16 20:21:40

我认为对于你的需求,你可以使用

代码语言:javascript
复制
HttpURLConnection.setReadTimeout()

请注意,这些链接将对您有所帮助。

  1. Is it possible to read from a Java InputStream with a timeout?
  2. Class HttpURLConnection
  3. Can I set a timeout for a InputStream's read() function?
票数 3
EN

Stack Overflow用户

发布于 2012-10-16 20:20:28

您也可以使用计时器,但我认为Handler解决方案是最优雅的。

看看这个:http://developer.android.com/reference/android/os/Handler.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12914484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档