Android: Doing networking on another thread

Comments 0

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();
0
❤️
0
👍
0
😲
0
😢
0
😠
0

Comments (0)

Please be civil when commenting. Think before writing, don't spam, self promote, bully, harass or harm anyone. Please read the Comment Policy before posting. Comments are moderated.

There are no comments.
Be the first to contribute!


Scroll to top