After entering the code from listing 26.12, the application crashes in getUrlBytes at the statement:

At that moment, “url” contains:

The crash I get is below. I found the definition of this exception here:

But I found that I cannot catch it (or do I need some special code for that?). Regardless, I cannot figure out why the exception is happening or how to fix it. FWIW, I plugged the url into my browser and it worked just fine (returned the xml I wanted). I’m stuck at this point and can’t move forward.

Here’s my version of getUrlBytes:

byte[] getUrlBytes(String urlSpec) throws IOException {
		URL url = new URL(urlSpec);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			InputStream in = connection.getInputStream();
			if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
				return null;
			int bytesRead = 0;
			byte[] buffer = new byte[1024];
			while ((bytesRead = > 0) {
				out.write(buffer, 0, bytesRead);
			return out.toByteArray();
		finally {

FWIW it fails exactly the same way using the downloaded solution (I just plugged in my API key)


…more on this:

The GaiException seems to be caused by asynchronous activity. If you are catching all RuntimeExceptions[including subclasses] you will hit this one when an async event fires. It appears to not be an error.

I wish this were explained better in the text.


I was confused - it looks like this is the debugger, right, not an actual crash?

This, by the way, is why we recommend not leaving caught exception breakpoints on all the time. There are a lot of places where caught exceptions are thrown in the frameworks, and it’d be impossible for us to enumerate them all.