2017-03-04 7 views
0

네트워크 스캐너를 구현하고 있습니다. ip 주소를 ping합니다. ping가 반환되면 Node 클래스를 사용하여 Node를 만듭니다. 그러나 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
  • 실행 후 pass my me THOD Void

그러나, 당신의 onProgressUpdate 방법은 Node 유형을 기대하고있다. 여기도 있습니다

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

좋은 예와 설명 : 그래서 TaskScanNetwork의 당신의 정의를 변경합니다. ! 당신이 이미 본 경우 사과는 : -이 대답은 당신을 위해 작동하는 경우 동의하는 것을 잊지 마세요 :)

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

+0

감사 – miatech

+0

순이익은 ... 버디 – brindy