How to Fix the Android NetworkOnMainThreadException

DroidIf you’re trying to communicate with a web service or scrape any data from the web in your Android application, you might run into an exception like this:

E/AndroidRuntime(673): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroidActivity}: android.os.NetworkOnMainThreadException

From Google’s documentation:

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it’s heavily discouraged.

The fix

The problem here is simply that you need to make your web service calls (or what-have-you) on a separate thread. So, quite simply, you’ll need to look into how to do threading with Android. Unfortunately this can be a bit of a pain because you need to make your service calls on a separate thread, but you need to update the UI on the main thread. Normally this would require passing data between the threads, which involves handlers or other complexities. Luckily the Android platform provides the Async Task to handle this, which alleviates some of this complexity and may help you avoid some clutter in your code.

Useful documentation to migrate your network calls to threads (or Android’s Async Task)

Painless Threading (from the Android Developer docs)
Async Task (from the Android Developer docs)
Android Threads, Handlers and AsyncTask – Tutorial
Designing for Responsiveness (from the Android Developer docs)

Good luck!

How to fix Error: suffix or operands invalid for ‘push’ or ‘pop’

TerminalIf you compile a 32-bit assembly program (or a C program that contains assembly instructions) on a 64-bit machine you may see the following error:

# as -o example32bit.o example32bit.s
example32bit.s: Assembler messages:
example32bit.s:10: Error: suffix or operands invalid for `pop'

To fix this problem you need to pass the compiler or assembler a flag telling it you want to compile the software as 32-bit and not 64-bit.

The fix for C: Compiling with the -m32 flag

If you’re using gcc to compile a C program, pass in -m32. There’s a complete example here on my blog.

The fix for assembly: Compiling with the –32 flag

If you’re using as to compile an assembly program, pass in –32. Then when you link with ld pass in -m elf_i386. There’s a complete example here on my blog.

Happy hacking!

My First Google Error

GoogleUntil now I’ve never actually had Google serve an error page to me. There have been times where I needed to reload Gmail or Google Plus, but today I saw a real error page delivered right to my browser by Google. It happened during a typical Google search and response was a 502, so I can only presume there was a problem with a back-end server that was grabbing my results. At first the instant search wasn’t working, then I was getting very limited results and finally I saw the pretty picture seen below. After about 15 seconds it was working completely normally again (that was fast Google!). Having been around so long and employing so many people I’m sure Google has some really great engineering going on to help with these sorts of failures. Cheers.