The solution to the android.os.NetworkOnMainThreadException exception.
At the beginning, the method of opening the connection of HttpURLConnection nection was placed in the UI thread, which may not be thread-safe, and the request of this method needs to wait, so this exception was thrown, and then the HttpURLConnection opened by the child thread, Everything is fine. Just start the child thread in the main thread.
Child thread exploits URL
problem solved. Paste a piece of code below
String file1 = SERVER_PATH; URL url = new URL(file1); HttpURLConnection httpconn = (HttpURLConnection) url .openConnection(); int responseCode = httpconn.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK) { Log.d(MP3, file1 + " server故障"); mainHandler.sendEmptyMessage(NET_SERVER_ERROR); }
The link is normal, after returning 200. You can open the stream to accept bytes.InputStream is = httpconn.getInputStream(); BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
...the latter will be omitted