public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) { Result<T> result = build(body); result.setCode(resultCodeEnum.getCode()); result.setMessage(resultCodeEnum.getMessage()); return result; }
时间: 2023-10-19 21:07:26 浏览: 49
这段代码是一个泛型静态方法,用于构建一个包含特定返回码和消息的 Result 对象,并将指定的 body 设置为 Result 对象的数据部分。
该方法的第一个参数 T body 是 Result 对象的数据部分,可以是任何类型的对象。第二个参数 ResultCodeEnum resultCodeEnum 是 Result 对象的返回码和消息的枚举对象,通过该参数我们可以指定 Result 对象的返回码和消息。该方法首先调用 build 方法创建一个 Result 对象,并将 body 设置为 Result 对象的数据部分,然后将 resultCodeEnum 中的返回码和消息分别设置为 Result 对象的返回码和消息,最后返回构建好的 Result 对象。
需要注意的是,该方法的泛型类型 T 是由第一个参数 body 的类型确定的,因此在调用该方法时需要指定 body 的类型,例如:Result<String> result = build("hello world", ResultCodeEnum.SUCCESS);
相关问题
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);
}
}
}
public static String getHtmlCode(String path) { String result = null; try { HttpClient httpclient = getHttpClient(); HttpGet get = new HttpGet(path); HttpResponse response = httpclient.execute(get); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent(), "utf-8")); String line; result = ""; while ((line = br.readLine()) != null) { result += line + "\n"; } } } catch(ConnectTimeoutException e) { System.out.println("ConnectTimeoutException timeout"); return null; } catch (SocketTimeoutException e) { System.out.println("SocketTimeoutException timeout"); return null; } catch (Exception e) { System.out.println(e.getMessage() + ":" + e.toString()); return null; } return result; } }我哪里错了?修改结果
这段代码也是使用的过时的HTTPClient,同样可能会导致编译错误。你可以使用OkHttp来实现相同的功能,如下所示:
```
public static String getHtmlCode(String path) {
String result = null;
OkHttpClient client = getHttpClient();
try {
Request request = new Request.Builder()
.url(path)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
result = response.body().string();
}
} catch (ConnectTimeoutException e) {
System.out.println("ConnectTimeoutException timeout");
return null;
} catch (SocketTimeoutException e) {
System.out.println("SocketTimeoutException timeout");
return null;
} catch (Exception e) {
System.out.println(e.getMessage() + ":" + e.toString());
return null;
}
return result;
}
```
这个代码使用OkHttp的Request和Response对象来获取HTML代码,并且也支持设置连接和读取超时时间。使用OkHttp可以更加方便地实现HTTP请求,同时也支持HTTP/2和HTTP/1.1协议。