AsyncTask<String ,Integer,Integer>类中各个方法的执行先后顺序

    首先,本例子是本人项目一个登录模块中的一小片代码段,并不是完整的代码。只是单纯说明了继承于AsyncTask类的子类对象中各个方法的执行先后顺序。

本例在UI中有以下语句:AnsLogin ansk=new AnsLogin();ansk.execute("");是对AnsLogin类的使用。

   

     /*

     * 生成该类的对象,并调用其execute()方法之后(AnsLogin ansk=new AnsLogin();ansk.execute("");)
     * 首先执行onPreExecute()方法
     * 其次是执行doInBackground()方法
     */
    class AnsLogin extends AsyncTask<String, Integer, Integer> {
        @Override
        // 该方法将在执行实际的后台操作前被UI thread调用(也就是前面所说的:调用其execute()方法之后,首先执行onPreExecute()方法),其运行在UI线程当中。
        //主要用于在异步操作之前做一些准备工作,如在页面出现转菊花提示:正在下载数据等
        protected void onPreExecute() {
            
            super.onPreExecute();
        }

      


        // 将在onPreExecute 方法执行后马上执行,该方法运行在后台线程中,因此不能在此方法中更新UI线程。
        // 这里将主要负责执行那些很耗时的后台计算工作,如请求服务器参数等。
        // 在该方法中,每次通过调用方法:publishProgress(i),都可以触发onProgressUpdate()方法
        //doInBackground(String... params)的参数为UI线程中调用execute()方法时所传递过来的参数,如本例中ansk.execute("")就传了空的字符串“”
        @Override
        protected Integer doInBackground(String... params) {
            
            resXML = ServiceInfo.Login(txtId.getText().toString(),txtPwd.getText().toString());

            int i = AnalysisXML.AnalysisResult(resXML).getResultCount();

            // publishProgress(i);//触发onProgressUpdate()方法,发布更新消息

            return i;

        }



        //该方法在doInBackground()方法执行完成之后再运行,并且运行在UI线程当中

        //onPostExecute(Integer result)里的参数为doInBackground()方法的返回值

        //主要用于将异步任务执行的结果展示给客户。

        @Override
        protected void onPostExecute(Integer result) {
            redirectMain(result);//这是一个页面跳转的方法,用于更新UI,此处不给出
        }



        //在doInBackground()方法中,每次调用publisProgress()方法之后,都会触发该方法,该方法执行在UI线程当中,因此可以更新UI

        //主要用于在异步执行的过程当中对用户进行提示,如更新进度条等

        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            System.out.println("onprogressUpdate=====>"+ Thread.currentThread().getName());
            super.onProgressUpdate(values);
            bar_seva.setProgress(values[0]);//更新进度条
        }
    }
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已经被弃用,请帮换一个写法
最新发布
07-08
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); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值