OkHttp sends a Post request with Json, throwing an exception android.os.NetworkOnMainThreadException

Problem Description

Throw an exception android.os.NetworkOnMainThreadException

problem causes

In Android 4.0 and above, the network connection method execute() cannot be placed on the main thread, otherwise the exception android.os.NetworkOnMainThreadException will be thrown. But the version under 4.0 may not report an error.

solution

Create a new thread for network requests, directly use the anonymous inner class to create a new thread

// 新开用于网络请求的线程
            new Thread(new Runnable(){
                @Override
                public void run() {
                    cachedImage = asyncImageLoader.loadDrawable(imageUrl, position);
                    imageView.setImageDrawable(cachedImage);
                }
            }).start();

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41170600/article/details/108902475