如何从http连接制作有时间限制的流

时间:2016-08-16 12:57:02

标签: java android http

如何修改此方法以使连接仅在有限的时间内保持连接?我希望在5秒连接后获得输出流。

 public boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
    HttpURLConnection urlConnection = null;
    try {
        final URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        InputStream stream = urlConnection.getInputStream();
        // you can use BufferedInputStream and BufferOuInputStream
        IOUtils.copy(stream, outputStream);
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(stream);
        Log.i(getTag(), "Stream closed all done");
        return true;
    } catch (final IOException e) {
        e.printStackTrace();
    }  finally {
        if (urlConnection != null)
            IOUtils.close(urlConnection);
    }
    return false;
}

0 个答案:

没有答案