本文介绍了如何解决AsyncTask的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序显示了 NetworkOnMainThreadException 在日志猫。
于是我找到了AsyncTask的方法来卸载网络操作后台线程。
加入AsyncTask.It节目后 ViewRootImpl $ CalledFromWrongThreadException 。所以我必须使用 onPostExecute 。结果
我学会了 Asyntack 文档和电子tried.Now我被困它没有返回正确output.I不知道我是否在宣布它各自的方法的。

My App shows a NetworkOnMainThreadException in the Log Cat.So I found the Asynctask Method to offload network operations to a background thread.After adding the AsyncTask.It shows ViewRootImpl$CalledFromWrongThreadException.So i have to use onPostExecute.
I learnt the Asyntack Docs & tried.Now i got Stuck it is not Returning the correct output.I don't know whether i declared it under the respective Method's.

我要显示在的TextView 电视处理的文本

I want to display the processed Text in the Textview tv

所以帮我在正确的方向:)

So Help me in the Right Direction :)

感谢您的帮助...

WifiApManager

public class WifiApManager {
     private final WifiManager mWifiManager;

public WifiApManager(Context context) {
    mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}

public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
    try {
        if (enabled) { // disable WiFi in any case
            mWifiManager.setWifiEnabled(false);
        }

        Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        return (Boolean) method.invoke(mWifiManager, wifiConfig, enabled);
    } catch (Exception e) {
        Log.e(this.getClass().toString(), "wifi", e);
        return false;
    }
}

public WIFI_AP_STATE getWifiApState() {
    try {
        Method method = mWifiManager.getClass().getMethod("getWifiApState");

        int tmp = ((Integer)method.invoke(mWifiManager));

        // Fix for Android 4
        if (tmp > 10) {
            tmp = tmp - 10;
        }

        return WIFI_AP_STATE.class.getEnumConstants()[tmp];
    } catch (Exception e) {
        Log.e(this.getClass().toString(), "wifi", e);
        return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
    }
}


public boolean isWifiApEnabled() {
    return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;
}


public WifiConfiguration getWifiApConfiguration() {
    try {
        Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");
        return (WifiConfiguration) method.invoke(mWifiManager);
    } catch (Exception e) {
        Log.e(this.getClass().toString(), "wifi", e);
        return null;
    }
}


public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) {
    try {
        Method method = mWifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
        return (Boolean) method.invoke(mWifiManager, wifiConfig);
    } catch (Exception e) {
        Log.e(this.getClass().toString(), "wifi", e);
        return false;
    }
}


public ArrayList<ClientScanResult> getClientList(boolean onlyReachables) {
    return getClientList(onlyReachables, 10);
}


public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) {
    BufferedReader br = null;
    ArrayList<ClientScanResult> result = null;

    try {
        result = new ArrayList<ClientScanResult>();
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");

            if ((splitted != null) && (splitted.length >= 4)) {
                // Basic sanity check
                String mac = splitted[3];

                if (mac.matches("..:..:..:..:..:..")) {
                    boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);

                    if (!onlyReachables || isReachable) {
                        result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.e(LOGTAG, e.toString());
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            Log.e(LOGTAG, e.toString());
        }
    }
return result;
}
}

connect.java

public class connect extends Activity{
WifiApManager wifiApManager;
TextView tv;
Button scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.connect);
    tv =(TextView) findViewById(R.id.iptv);
scan myScan = new scan(this); // pass the context to the constructor
    myScan.execute();
}
class scan extends AsyncTask<String, Void, Void> {
    public Context context;
    ArrayList<ClientScanResult> clients;
    public scan(Context c)  // constructor to take Context
    {
        context = c;   // Initialize your Context variable
    }

    protected Void doInBackground(String... params) {
            wifiApManager = new WifiApManager(context);  // use the variable here
             clients = wifiApManager.getClientList(false);
            return null;
    }
    }
    protected void onPostExecute(Void result){
        ArrayList<ClientScanResult> clients;
        tv.setText("WifiApState: " + wifiApManager.getWifiApState() + "\n\n");
        tv.append("Clients: \n");
for (ClientScanResult clientScanResult : clients) //showin error in clients
     {
                tv.append("####################\n");
                tv.append("IpAddr: " + clientScanResult.getIpAddr() + "\n");
                tv.append("Device: " + clientScanResult.getDevice() + "\n");
                tv.append("HWAddr: " + clientScanResult.getHWAddr() + "\n");
                tv.append("isReachable: " + clientScanResult.isReachable()+ "\n");
                           }
    }
    }

注意:
它显示了在客户端上的变量的方法onPostExecute在connect.java一个错误。

Note:It shows a error in connect.java on onPostExecute Method in Clients Variable.

推荐答案

在您的onPostExecute(......),你有

in your onPostExecute(...) you have

ArrayList<ClientScanResult> **clients**;

然后

for (ClientScanResult clientScanResult : **clients**) //showin error in clients

客户为空,因此永远不会执行循环。你不需要定义一个新的ArrayList,因为你已经有一个在扫描类定义为一个全局变量

clients is null therefore the loop is never executed. you don't need to define a new ArrayList since you already have one defined as a global variable in the scan class

有一个更好的办法来做到这一点是设置doInBackground的返回类型为ArrayList的&LT; ClientScanResult>然后你有onPostExecute采取一个ArrayList&LT; ClientScanResult>

A better way to do this is to set the return type of doInBackground to ArrayList< ClientScanResult> and then you have onPostExecute take an ArrayList< ClientScanResult>

class scan extends AsyncTask<Void, Void, ArrayList<ClientScanResult>> {

protected ArrayList<ClientScanResult> doInBackground(Void... params) {
    wifiApManager = new WifiApManager(context);  // use the variable here
    return wifiApManager.getClientList(false);
}

protected void onPostExecute(ArrayList<ClientScanResult> clients){

tv.setText("WifiApState: " + wifiApManager.getWifiApState() + "\n\n");
tv.append("Clients: \n");
for (ClientScanResult clientScanResult : clients) //showin error in clients
    {
        tv.append("####################\n");
        tv.append("IpAddr: " + clientScanResult.getIpAddr() + "\n");
        tv.append("Device: " + clientScanResult.getDevice() + "\n");
        tv.append("HWAddr: " + clientScanResult.getHWAddr() + "\n");
        tv.append("isReachable: " + clientScanResult.isReachable()+ "\n");
    }
}

这篇关于如何解决AsyncTask的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:08