2017-03-04 34 views
0

我正在實現網絡掃描器。其中ping一個IP地址。如果返回ping,則使用節點類創建節點。但是,嘗試在doinbackground()中使用publishupdate方法時出現錯誤。另外,onprogressupdate()方法說不覆蓋超類。實施有什麼問題。我正在閱讀文檔,但無法弄清楚。任何幫助讚賞。由於我該如何修復Asynctask類的publishupdate方法

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

通過的錯誤,我想你的意思是編譯錯誤。 AsyncTask是定義多個通用參數的泛型類型。想想的定義如下:

AsyncTask<BACKGROUND, PROGRESS, POST_EXECUTE> 

您目前有:

AsyncTask<Void, String, Void> 

所以你說:

  • 在後臺通過我的方法Void
  • 進展通我的方法String
  • on post執行通過我的我的ThOD Void

然而,你onProgressUpdate方法需要另外一種類型Node。因此,改變你的TaskScanNetwork到的定義:

private class TaskScanNetwork extends AsyncTask<Void, Node, Void> { 

也有一個很好的例子,說明在這裏。道歉,如果你已經看到了這個已經:

https://developer.android.com/reference/android/os/AsyncTask.html#AsyncTask()

+0

謝謝哥們...... – miatech

+0

NP - 不要忘了,如果接受這個答案對你的作品!:) – brindy