I am comfortable with actionscript. When I wantet to have network access for example in air, I coded the request and waited for the EventListener to kick in.
At the moment I’m doing a java tutorial with the following android code (wrapped in a try/catch):
url = new URL("http://www.google.com");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream());
String line = '';
while((line = reader.readLine()) != null) {
// do something with the line
}
I don’t get how this works.
Why is there no eventListener needed? If the code proceeds immidiately after the URL is requested, the URL may not been retrieved when the while-Construct kicks in.
Is the app just paused in the meantime? If yes, is it possible to realise this with an eventlistner?
Thank you for help!
Yes. The regular Java IO classes are synchronous. The call to
readLine()will block until either it can return a line or there’s an Exception.Yes, by using classes in the
java.niopackage. But only with a somewhat lower-level interface.In general, Java applications use multithreading instead of event qeueues to deal with such sitatuations.