Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The problem of how to use AsyncTask in Android

Shulou Source: shulou.com Published: 2022-06-02 08:48:34 10月27日 Update

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.

Tags: Questions data code methods content functions customers clients more articles face-to-face help testing yes picture above picture below thing use main program information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia Microsoft Huawei Xiaomi