0

I read Google provides 2 different examples of HttpURLConnection usage - Should we call HttpURLConnection's disconnect, or InputStream's close? and I know use close() on Android is correct. But, when I close connection with InputStream'clos(), it cost 10 second to finish. I want finish to close connection in 1s. How do I do?

    private HttpURLConnection getConnection(String uri) throws IOException {
    try {
        URL url = new URL(ROOT_URL + uri);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        connection.connect();

        DataInputStream dIS = new BufferedInputStream(connection.getInputStream(), 100000);

        dIS.close(); //it will finish over 10s

    } catch (IOException e) {
    }
}
Community
  • 1
  • 1
alingogo
  • 1
  • 1
  • 1
    What? closing should never cost 10s, how are you measuring that and what is the code involved? – zapl Nov 26 '14 at 02:24
  • I add code with InputStream'close(). There may be a wrong code. but I don't understand. – alingogo Nov 26 '14 at 02:41
  • Why is your buffer size `10000`? That is in bytes, not milliseconds. – motoku Nov 26 '14 at 02:48
  • It's probably consuming the input. Are you sure no exception is thrown? If you need to just test the URL, don't get the input stream and close it, get the response code. Or, you could try setting a one second read timeout. – user207421 Nov 26 '14 at 03:05
  • @EJP I get the input stream for deal motionjpeg stream. – alingogo Nov 26 '14 at 04:13
  • But you're closing it immediately! What's the purpose of the code you posted? *Or*, is there more code that you haven't posted? All you're testing here is how long it takes for the `Streams.skipAll(responseBodyIn);` call mentioned in the other thread to execute. If you read the input instead, skipAll()` doesn't do anything so `close()`. won't take ten seconds. – user207421 Nov 26 '14 at 04:50

0 Answers0