• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps not getting listview adapter populated from ipScanActivity

zentech25

Lurker
I have the following IpScanActivity when I populate the listView adapter with fake data, it works, but when I run it with the try/catch doInbackground() method it is not showing anything. I can see the host increasing in the logs. Is it the time out? I have more hosts on my network, so it is not that. any ideas.. thanks
IpScanActivity

Code:
public class IpScanActivity extends AppCompatActivity {

   String ip;
   String mac;
   ArrayList<Node> hostList;
   ListView networkScan;

   [USER=1021285]@override[/USER]
   protected void onCreate([USER=1996173]@nullable[/USER] Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.scan_list);

       hostList = new ArrayList<>();

       //inflating adapter
       networkScan  = (ListView) findViewById(R.id.scan_list);
       NodeAdapter networkAdapter = new NodeAdapter(this, R.layout.list_item, hostList);
       networkScan.setAdapter(networkAdapter);

       //scanning network
       TaskScanNetwork scanNetwork = new TaskScanNetwork();
       scanNetwork.execute();



   }

Code:
   /*
      * AscynTask to scan the network
      * you should try different timeout for your network/devices
      * it will try to detect localhost ip addres and subnet. then
      * it will use subnet to scan network
      *
      */
   private class TaskScanNetwork extends AsyncTask<Void, Node, Void> {

       String localIp;
       String mac;
       String subnet;
       static final int lower = 1;
       static final int upper = 254;
       static final int timeout = 2000;

       [USER=1021285]@override[/USER]
       protected void onPreExecute() {
           WifiManager wifiMan1 = (WifiManager) getSystemService(WIFI_SERVICE);
           WifiInfo wifiInfo1 = wifiMan1.getConnectionInfo();
           int ipAddress = wifiInfo1.getIpAddress();
           localIp  = String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
           Log.v("Ip Address: ", localIp);
           subnet = localIp.substring(0, localIp.lastIndexOf("."));
           hostList.clear();
       }

       [USER=1021285]@override[/USER]
       protected Void doInBackground(Void... params) {
           for (int i = lower; i <= upper; i++) {
               ip = subnet + "." + i;
               Log.v("Host: ", ip);
               mac = "00:11:22:33:44:" + i;

               /*Node newNode = new Node(ip, "00:11:22:33:44:55");
               publishProgress(newNode);*/

               try {
                   InetAddress inetAddress = InetAddress.getByName(ip);
                   if (inetAddress.isReachable(timeout)) {
                       Node newNode = new Node(ip, mac);
                       publishProgress(newNode);
                   }

               } catch (UnknownHostException e) {
                   e.printStackTrace();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }

           return null;
       }

       [USER=1021285]@override[/USER]
       protected void onProgressUpdate(Node... values) {
           hostList.add(values[0]);
           networkScan.deferNotifyDataSetChanged();
       }

       [USER=1021285]@override[/USER]
       protected void onPostExecute(Void aVoid) {
           Toast.makeText(getApplicationContext(), "Scan Completed!", Toast.LENGTH_LONG);
       }
   }

}
 
Last edited by a moderator:
Hi. Welcome to Android Forums.

A couple of observations about your code:
First the logging output in your catch blocks will likely disappear into nowhere

Code:
e.printStackTrace()

So if an exception is caught, you won't know anything about it.
Instead use Log.d or Log.v as you have done in other parts of the code

If that doesn't reveal anything, then the next step is to run your application in debug mode, and set breakpoints in the code. I would start by setting a breakpoint at the first line of doInBackground(). Then step through line by line, to see what happens.

If you read the following post, you'll find some advice on debugging techniques

http://androidforums.com/threads/please-read-me-before-posting.987318/
 
Back
Top Bottom