我正在实施网络扫描程序。 ping IP地址。如果返回ping,则使用Node类创建Node。但是,当尝试在doinbackground()中使用publishupdate方法时,我会收到错误。另外,onprogressupdate()方法表示不覆盖超类。实施有什么问题。我正在阅读文档,但无法弄清楚。任何帮助赞赏。感谢
package com.example.android.droidscanner;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jlvaz on 3/1/2017.
*/
public class IpScanActivity extends AppCompatActivity {
String ip;
String mac;
ArrayList<Node> hostList;
ListView networkScan;
@Override
protected void onCreate(@Nullable 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();
}
/*
* 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, String, Void> {
String localIp;
String mac;
String subnet;
static final int lower = 1;
static final int upper = 254;
static final int timeout = 1000;
@Override
protected void onPreExecute() {
WifiManager wifiMan = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMan.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
localIp = String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
subnet = ip.substring(0, ip.lastIndexOf("."));
hostList.clear();
//textResult.setText("Scanning Network...");
}
@Override
protected Void doInBackground(Void... params) {
for (int i = lower; i <= upper; i++) {
String host = subnet + "." + i;
Log.v("Host: ", host);
try {
InetAddress inetAddress = InetAddress.getByName(host);
if (inetAddress.isReachable(timeout)) {
Node newNode = new Node(host);
publishProgress(newNode);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Node... values) {
hostList.add(values[0]);
networkScan.deferNotifyDataSetChanged();
}
@Override
protected void onPostExecute(Void aVoid) {
//result.setText("Live Hosts: ");
}
}
}
节点类
package com.example.android.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 remark;
boolean isReachable;
Node(String ip){
this.ip = ip;
//this.mac = mac;
this.isReachable = true;
}
public String getIp() {
return ip;
}
public String getHostName() {
return HostName;
}
public String getMac() {
return mac;
}
public String getRemark() {
return remark;
}
public boolean isReachable() {
return isReachable;
}
@Override
public String toString() {
return "IP: " + ip + "\n" +
"MAC: " + mac + "\n" +
"CanonicalHostName:\t" + CanonicalHostName + "\n" +
"HostName:\t" + HostName + "\n" +
"isReachable: " + isReachable +
"\n" + remark;
}
private void queryHost(){
try {
InetAddress inetAddress = InetAddress.getByName(ip);
CanonicalHostName = inetAddress.getCanonicalHostName();
HostName = inetAddress.getHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
remark = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
remark = e.getMessage();
}
}
}
NodeAdapter类
public class NodeAdapter extends ArrayAdapter<Node>{
public NodeAdapter(Context context, int num, ArrayList<Node> allHost) {
super(context, 0, allHost);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Node host = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
// Lookup view for data population
TextView nodeIpTxtView = (TextView) convertView.findViewById(R.id.node_ip);
TextView nodeMacTxtView = (TextView) convertView.findViewById(R.id.node_mac);
// Populate the data into the template view using the data object
nodeIpTxtView.setText(host.getIp());
nodeMacTxtView.setText(host.getMac());
// Return the completed view to render on screen
return convertView;
}
}
答案 0 :(得分:0)
错误,我认为你的意思是编译错误。 AsyncTask
是一种泛型类型,它定义了许多通用参数。可以将定义想象如下:
AsyncTask<BACKGROUND, PROGRESS, POST_EXECUTE>
您目前有:
AsyncTask<Void, String, Void>
所以你说:
Void
String
Void
但是,您的onProgressUpdate
方法需要Node
类型。因此,请将TaskScanNetwork
的定义更改为:
private class TaskScanNetwork extends AsyncTask<Void, Node, Void> {
这里也有一个很好的例子和解释。如果您已经看过这个,请道歉:
https://developer.android.com/reference/android/os/AsyncTask.html#AsyncTask()