Android: Doing networking on another thread - Milan Kragujević

Android: Doing networking on another thread


Beware This post is more than 3 years old, it may be outdated or incorrect! Please check elsewhere for accurate information!

Since Honeycomb (3.x), Google has prevented apps to run networking operations on the main thread, since it would cause the app to freeze while it's waiting for the network operation to finish.

There aren't many easy solutions to understand, and many use tons of code and usually extend the class AsyncTask, which is needless. If you only need to fetch text resources from the Internet, this code below will do.

Basically put your existing code inside the try/catch block and hope it'll work :D

Thread thread = new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            // PUT CODE HERE!
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

thread.start();

This post was last updated on February 8th, 2014.


0
❤️
0
👍
0
😲
0
😢
0
😠
0

Loading...

Scroll to top