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已经被弃用,请帮换一个写法
时间: 2024-02-14 07:32:11 浏览: 90
S-Inventory Equipment, Crafting, Skill Bar, Containers & Merchant.unitypackage
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);
}
}
}
阅读全文