This is the case. The poster's result(InputStream) is an InputStream pointing to a remote resource. Reading data from the InputStream at this time is equivalent to reading the remote resource, so an error will be reported. The fundamental reason is that Android's restriction on "network requests cannot be executed in the main thread" is the entire data process, not just limited to the connection ((HttpURLConnection)url.openConnection()).
So the recommended approach is to place the entire InputStream parsing in doInBackground, and then return an entity class that conforms to the business logic in doInBackground.
This is the case. The poster's result(InputStream) is an InputStream pointing to a remote resource. Reading data from the InputStream at this time is equivalent to reading the remote resource, so an error will be reported. The fundamental reason is that Android's restriction on "network requests cannot be executed in the main thread" is the entire data process, not just limited to the connection ((HttpURLConnection)url.openConnection()).
So the recommended approach is to place the entire InputStream parsing in doInBackground, and then return an entity class that conforms to the business logic in doInBackground.
Parsing is a time-consuming operation and should be placed in a sub-thread,
onPostExecute
是主线程,doInBackground
is a sub-thread.You did a time-consuming operation on the main thread, so it’s normal to report an error.
You should replace the InputStream with the obtained data String
Then parse in onPostExecute()