In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to use AsyncTask in Android, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Problem description
I'm just going to write this code for AsyncTask here, and I'll talk about it.
@ Override protected String doInBackground (String... Strings) {String tranname=strings [0]; String transdata=strings [1]; String recvData= ""; try {/ / Connect SOCKET client socketinfo=dataTransSocket.SocketConnect (msyscfg); if (socketinfo.retno! =-1) {clientData=new CSocketClientData (); clientData.transName=tranname; clientData.data=transdata / / get data recvData=dataTransSocket.SocketSenduntilRecvStr (clientData); dataTransSocket.SocketDisconnect ();} else {recvData=socketinfo.retmsg;}
} catch (Exception e) {e.printStackTrace (); Log.i ("socket", e.getMessage ()); recvData=e.getMessage ();} return recvData;}
@ Override protected void onPostExecute (String recvdata) {super.onPostExecute (recvdata)
Mnetinfocallback.onNetinfoCallBackOver (clientData.transName, recvdata)
}
Callback method of main program interface
In the above code, after Socket obtains the data under normal circumstances, you can directly call the following callback method to pass the data back. In the later testing process, we shut down the server of Socket and let its Socket connection fail. As shown in the figure above, you should directly use Toast to prompt the returned information in the callback function.
As a result, we found in the test that the program crashed directly after the Socket connection failed, as shown in the following figure
During debugging, we opened the LogCat and took a look at it. It said the problem of onPostExecute.
Then we added breakpoints to track, and found that after enabling the callback function, it did not come into the main process, and then collapsed. Here, I directly looked for relevant information on the Internet and found nothing. But in an article, I saw the following picture saying
Solution method
According to the above understanding, let's make an intermediate conversion of the output parameters and try again, and the changed code is
@ Override protected String doInBackground (String... Strings) {String tranname=strings [0]; String transdata=strings [1]; String recvData= ""; try {/ / Connect SOCKET client socketinfo=dataTransSocket.SocketConnect (msyscfg); if (socketinfo.retno! =-1) {clientData=new CSocketClientData (); clientData.transName=tranname; clientData.data=transdata / / get data recvData=dataTransSocket.SocketSenduntilRecvStr (clientData); recvData=mnetinfocallback.Success + "|" + recvData; dataTransSocket.SocketDisconnect ();} else {recvData=mnetinfocallback.Fail + "|" + socketinfo.retmsg;}
} catch (Exception e) {e.printStackTrace (); Log.i ("socket", e.getMessage ()); recvData=mnetinfocallback.Fail + "|" + e.getMessage ();} return recvData;}
@ Override protected void onPostExecute (String recvdata) {super.onPostExecute (recvdata)
String [] strings=recvdata.split ("\\ |"); if (strings [0] .equals (mnetinfocallback.Success)) {if (! strings [1] .equals ("")) {Log.i ("post", strings [1]); mnetinfocallback.onNetinfoCallBackOver (clientData.transName, strings [1]) }} else {mnetinfocallback.onNetinfoCallBackOver (mnetinfocallback.Fail, strings [1]);}}
After we re-ran it, we found that there was a problem with the normal pop-up.
This is the end of the question about how to use AsyncTask in Android. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.