protected void onPreExecute() { //在异步任务之前,在主线程中 super.onPreExecute(); } @Override protected TreeMap<String, String> doInBackground(String... strings) { //在另一个线程中处理事件 Search search = new Search(); TreeMap<String, String> result = search.search(strings[0]); return result; }
时间: 2024-02-14 13:08:08 浏览: 34
这段代码是一个异步任务(AsyncTask)类的定义。异步任务在Android中常用于在后台执行长时间运行的操作,比如网络请求、数据读写等等,以避免阻塞UI主线程,从而提高应用程序的响应速度和用户体验。
在这个异步任务中,onPreExecute()方法是执行异步任务之前在主线程中执行的方法,可以用于任务执行前的一些准备工作。而doInBackground()方法是在另一个线程中执行的方法,用于实际的异步操作,如网络请求等等。在这个例子中,doInBackground()方法创建了一个Search对象,然后调用该对象的search()方法进行搜索操作,并将搜索结果保存在一个TreeMap中,最终返回这个TreeMap对象。需要注意的是,这里的参数类型是String数组,表示可以传入多个参数。
需要注意的是,在AsyncTask类中还可以定义其他方法,比如onPostExecute()方法,它是在异步任务执行完毕后在主线程中执行的方法,用于处理异步任务的结果。
需要提醒的是,在Android 11及之后的版本,Google官方不再推荐使用AsyncTask,而是推荐使用更加灵活和可控的协程(Coroutine)来实现异步任务。
相关问题
package com.hmongsoft.merchant.Module.dataSource.onLine.V20230707; import android.os.AsyncTask; import com.hmongsoft.merchant.Base.Interface.ActionCallbackValue; import com.hmongsoft.merchant.Base.config.SysConfig; import java.io.IOException; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; /** * 校验商铺名称是否存在 */ public class JudgeMerchantIsExist { public JudgeMerchantIsExist(String merchantName, ActionCallbackValue<String,Boolean> actionCallback) { SignInTask signInTask=new SignInTask(merchantName,actionCallback); signInTask.execute(); } private static class SignInTask extends AsyncTask<String,Integer,Boolean>{ private String merchantName; private ActionCallbackValue<String,Boolean> actionCallback; private String requestResult; public SignInTask(String merchantName, ActionCallbackValue<String,Boolean> actionCallback) { this.merchantName =merchantName; this.actionCallback=actionCallback; } //异步前(UI) @Override protected void onPreExecute() { super.onPreExecute(); } //异步中(非UI) @Override protected Boolean doInBackground(String... strings) { OkHttpClient client = new OkHttpClient().newBuilder().build(); RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM) .addFormDataPart("merchantName",merchantName) .build(); Request request = new Request.Builder() .url(SysConfig.MerchantPORT+"/MerchantController/judgeMerchantNameIsExist") .method("POST", body) .build(); try { requestResult = client.newCall(request).execute().body().string(); if (requestResult.equals("true")){ return true; }else { return false; } } catch (IOException e) { e.printStackTrace(); return false; } } //异步后(UI) @Override protected void onPostExecute(Boolean aBoolean) { super.onPostExecute(aBoolean); actionCallback.callback("result",aBoolean); } } } 这段代码的SignInTask已经被弃用,请帮换一个写法
package com.hmongsoft.merchant.Module.dataSource.onLine.V20230707;
import android.os.AsyncTask;
import com.hmongsoft.merchant.Base.Interface.ActionCallbackValue;
import com.hmongsoft.merchant.Base.config.SysConfig;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
/**
* 校验商铺名称是否存在
*/
public class JudgeMerchantIsExist {
public JudgeMerchantIsExist(String merchantName, ActionCallbackValue<String, Boolean> actionCallback) {
new SignInTask(merchantName, actionCallback).execute();
}
private static class SignInTask extends AsyncTask<Void, Void, Boolean> {
private String merchantName;
private ActionCallbackValue<String, Boolean> actionCallback;
public SignInTask(String merchantName, ActionCallbackValue<String, Boolean> actionCallback) {
this.merchantName = merchantName;
this.actionCallback = actionCallback;
}
@Override
protected Boolean doInBackground(Void... voids) {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
String json = "{\"merchantName\":\"" + merchantName + "\"}";
RequestBody body = RequestBody.create(json, mediaType);
Request request = new Request.Builder()
.url(SysConfig.MerchantPORT + "/MerchantController/judgeMerchantNameIsExist")
.post(body)
.build();
try {
String response = client.newCall(request).execute().body().string();
return response.equals("true");
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
actionCallback.callback("result", result);
}
}
}
9. AsyncTask类的哪些方法在主线程中执行?哪些方法在子线程中执行?
在Android中,AsyncTask类是一种异步任务处理类,可以在后台线程中执行耗时操作,然后将执行结果返回到主线程更新UI,避免了在主线程中执行耗时操作导致UI卡顿的问题。AsyncTask类中的方法在主线程和子线程中执行情况如下:
在主线程中执行的方法:
- onPreExecute(),在执行异步任务前被调用,一般用于做一些初始化工作;
- onProgressUpdate(Progress...),在异步任务执行过程中被调用,用于更新UI进度;
- onPostExecute(Result),在异步任务执行完成后被调用,用于更新UI结果。
在子线程中执行的方法:
- doInBackground(Params...),在执行异步任务时被调用,用于执行耗时操作。
需要注意的是,AsyncTask类默认的线程池中只有一个后台线程,如果需要执行多个异步任务,需要调用executeOnExecutor方法来使用自定义线程池。同时,在使用AsyncTask类时,需要特别关注内存泄漏问题,避免在Activity或Fragment中持有匿名内部类的引用。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)