Skip to content Skip to sidebar Skip to footer

How To Troubleshoot Asynctask?

My App shows a NetworkOnMainThreadException in the Log Cat. So I found the Asynctask Method to offload network operations to a background thread. After adding the AsyncTask.It show

Solution 1:

in your onPostExecute(...) you have

ArrayList<ClientScanResult> **clients**;

and then

for (ClientScanResult clientScanResult : **clients**) //showin error in clients

clients is null therefore the loop is never executed. you don't need to define a new ArrayList since you already have one defined as a global variable in the scan class

A better way to do this is to set the return type of doInBackground to ArrayList< ClientScanResult> and then you have onPostExecute take an ArrayList< ClientScanResult>

classscanextendsAsyncTask<Void, Void, ArrayList<ClientScanResult>> { 

protected ArrayList<ClientScanResult> doInBackground(Void... params) {
    wifiApManager = new WifiApManager(context);  // use the variable herereturn wifiApManager.getClientList(false);
}

protected void onPostExecute(ArrayList<ClientScanResult> clients){

tv.setText("WifiApState: "+ wifiApManager.getWifiApState() +"\n\n");
tv.append("Clients: \n");
for (ClientScanResult clientScanResult : clients) //showin error in clients
    {
        tv.append("####################\n");
        tv.append("IpAddr: "+ clientScanResult.getIpAddr() +"\n");
        tv.append("Device: "+ clientScanResult.getDevice() +"\n");
        tv.append("HWAddr: "+ clientScanResult.getHWAddr() +"\n");
        tv.append("isReachable: "+ clientScanResult.isReachable()+"\n");
    }
}

Post a Comment for "How To Troubleshoot Asynctask?"