gpt4 book ai didi

java - 如何排除异步任务故障?

转载 作者:行者123 更新时间:2023-12-01 23:18:44 26 4
gpt4 key购买 nike

我的应用程序在 Log Cat 中显示 NetworkOnMainThreadException。所以我找到了 Asynctask 方法来将网络操作卸载到后台线程。添加 AsyncTask 后,它显示 ViewRootImpl$CalledFromWrongThreadException。所以我必须使用 onPostExecute
我学习了Asyntack文档并尝试过。现在我被卡住了,它没有返回正确的输出。我不知道我是否在相应的方法下声明了它。

我想在Textview电视中显示处理后的文本

所以请帮助我走向正确的方向:)

感谢您的帮助...

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");
}
}
}

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

最佳答案

在你的 onPostExecute(...) 中你有

ArrayList<ClientScanResult> **clients**;

然后

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

clients 为 null,因此循环永远不会执行。您不需要定义一个新的 ArrayList,因为您已经在扫描类中定义了一个全局变量

更好的方法是将 doInBackground 的返回类型设置为 ArrayList< ClientScanResult>,然后让 onPostExecute 获取 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");
}
}

关于java - 如何排除异步任务故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20845369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com