我得到Exception
在Runtime
與AsyncTask
類。但是我無法準確地確定在發生這種情況時在BufferReader
while循環中的位置。我可以在調試器中看到讀取文件的第一行,然後在設備上讀取第二行,但是我認爲在創建節點對象或更新進度的這一點是拋出Exception
時。任何幫助表示讚賞。在運行AsyncTask時得到運行時異常
package com.example.george.droidscanner;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button btnRead;
TextView textResult;
ListView listViewNode;
ArrayList<Node> listNote;
ArrayAdapter<Node> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRead = (Button)findViewById(R.id.readclient);
textResult = (TextView)findViewById(R.id.result);
listViewNode = (ListView)findViewById(R.id.nodelist);
listNote = new ArrayList<>();
ArrayAdapter<Node> adapter = new ArrayAdapter<Node>(
MainActivity.this,
android.R.layout.simple_list_item_1,
listNote);
listViewNode.setAdapter(adapter);
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TaskReadAddresses(listNote, listViewNode).execute();
}
});
}
private class TaskReadAddresses extends AsyncTask<Void, Node, Void> {
ArrayList<Node> array;
ListView listView;
TaskReadAddresses(ArrayList<Node> array, ListView v){
listView = v;
this.array = array;
array.clear();
textResult.setText("querying...");
}
@Override
protected Void doInBackground(Void... params) {
readAddresses();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
textResult.setText("Done");
}
@Override
protected void onProgressUpdate(Node... values) {
listNote.add(values[0]);
((ArrayAdapter)(listView.getAdapter())).notifyDataSetChanged();
}
private void readAddresses() {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
Node thisNode = new Node(ip, mac);
publishProgress(thisNode);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
節點類
package com.example.george.droidscanner;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Created by george on 2/14/17.
*/
public class Node {
String ip;
String mac;
String CanonicalHostName;
String HostName;
String LocalHostCanonicalHostName;
String LocalHostHostName;
String remark;
boolean isReachable;
Node(String ip, String mac){
this.ip = ip;
this.mac = mac;
queryHost();
}
@Override
public String toString() {
return "IP: " + ip + "\n" +
"MAC: " + mac + "\n" +
"CanonicalHostName:\t" + CanonicalHostName + "\n" +
"HostName:\t" + HostName + "\n" +
"getLocalHost().getCanonicalHostName():\t" + LocalHostCanonicalHostName + "\n" +
"getLocalHost().getHostName():\t" + LocalHostHostName + "\n" +
"isReachable: " + isReachable +
"\n" + remark;
}
private void queryHost(){
try {
InetAddress inetAddress = InetAddress.getByName(ip);
CanonicalHostName = inetAddress.getCanonicalHostName();
HostName = inetAddress.getHostName();
LocalHostCanonicalHostName = inetAddress.getLocalHost().getCanonicalHostName();
LocalHostHostName = inetAddress.getLocalHost().getHostName();
isReachable = inetAddress.isReachable(3000);
} catch (UnknownHostException e) {
e.printStackTrace();
remark = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
remark = e.getMessage();
}
}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.example.george.droidscanner.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<Button
android:id="@+id/readclient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Read Ip/MAC addresses"/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textSize="24sp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nodelist">
</ListView>
</LinearLayout>
運行時異常錯誤
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.george.droidscanner, PID: 18522
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
at java.net.InetAddress.getLocalHost(InetAddress.java:396)
at com.example.george.droidscanner.Node.queryHost(Node.java:44)
at com.example.george.droidscanner.Node.<init>(Node.java:24)
at com.example.george.droidscanner.MainActivity$TaskReadAddresses.readAddresses(MainActivity.java:95)
at com.example.george.droidscanner.MainActivity$TaskReadAddresses.doInBackground(MainActivity.java:67)
at com.example.george.droidscanner.MainActivity$TaskReadAddresses.doInBackground(MainActivity.java:53)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
at libcore.io.Posix.android_getaddrinfo(Native Method)
at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
at java.net.InetAddress.getLocalHost(InetAddress.java:396)
at com.example.george.droidscanner.Node.queryHost(Node.java:44)
at com.example.george.droidscanner.Node.<init>(Node.java:24)
at com.example.george.droidscanner.MainActivity$TaskReadAddresses.readAddresses(MainActivity.java:95)
at com.example.george.droidscanner.MainActivity$TaskReadAddresses.doInBackground(MainActivity.java:67)
at com.example.george.droidscanner.MainActivity$TaskReadAddresses.doInBackground(MainActivity.java:53)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
這是問題之一,但不是主要問題。注意在那之前有一個例外。你是否運行它並且工作?...因爲我剛剛添加了權限並仍然有例外 – miatech
我的不好,我在錯誤的地方添加了權限...謝謝 – miatech