Hello,
I get some response from calling a website, basically I do:
That works fine. Now in the following code I haveCode:InputStream response = null; HttpSessionToken = (HttpURLConnection) new URL (MyUrl).openConnection(proxy); response = (InputStream)HttpSessionToken.getContent();
Code://some code here... result = stringifyInputStream(response) ; // line 1 //some more code here... response.close(); // line 2 return response;
and this is the method being called
In the above code I have marked two lines as line 1 and line 2, these are the lines in question. The "return response" returns nothing as long as line 1 and 2 exist. If I remove line 1 and 2 then the return statement works fine.Code:public static String stringifyInputStream(InputStream inputStream) throws IOException { int ichar; ByteArrayOutputStream baos = new ByteArrayOutputStream(); for(;;) { ichar = inputStream.read(); if(ichar < 0) { break; } baos.write(ichar); } return baos.toString("UTF-8"); }
The stringify Method only reads the inputStream why does it destroy it? Why does a close() destroys my stream. I need the stream later on and if I don't close it it will stay open for ever, not sure if that really harms, but I guess I have to close what I open before.
Any help is appreciated. Thanks.




Reply With Quote