Issue
I'm using AsyncTask to read "/proc/net/arp" file after it's been populated. the problem is that after I read each line I want to do name resolution for every ip address. and that's when I'm getting exception errors. I'm not executing this on main thread as I'm using AsyncTask for this process. this is my onProgressUpdate method
protected void onProgressUpdate(Void... params) {
//update progress bar
/*scanProgress.setProgress(values[0].progressBar);*/
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(ARP_TABLE), "UTF-8"));
reader.readLine(); // Skip header.
String line;
while ((line = reader.readLine()) != null) {
Log.v("ARPFILE", line);
String[] arpLine = line.split("\\s+");
//if arp line contains flag 0x2 we parse host
if(arpLine[2].equals(ARP_FLAG)) {
final String ip = arpLine[0];
final String flag = arpLine[2];
final String mac = arpLine[3];
Node node = new Node(ip, mac);
hostList.add(node);
networkAdapter.notifyDataSetInvalidated();
// scanProgress.setProgress(node.progressBar);
}
}
reader.close();
}
catch (Exception ex) {
}
for(Node node: hostList) {
InetAddress inetAddress;
android.os.Debug.waitForDebugger();
try {
inetAddress = InetAddress.getByName(node.ip);
String hostname = inetAddress.getCanonicalHostName();
node.setHostName(hostname);
Log.v("HOSTNAME", hostname);
} catch (UnknownHostException e) {
// It's common that many discovered hosts won't have a NetBIOS entry.
}
}
}
Solution
I'm not executing this on main thread as I'm using AsyncTask for this process. this is my onProgressUpdate method
The onProgressUpdate()
method is called on the main thread. From the Documentation for AsyncTask
:
onProgressUpdate(Progress...)
, invoked on the UI thread after a call topublishProgress(Progress...)
. The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
Execute any code that should be done off the UI Thread in doInBackground()
Answered By - PPartisan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.