Android异步任务类
我可以像在C#中一样在android中使用await for async类吗 我想等待我的消息。这是一个回调方法。返回NewStocktakin值后,我必须在第行下面继续。有可能吗?因为我的存货ID返回0Android异步任务类,android,android-asynctask,Android,Android Asynctask,我可以像在C#中一样在android中使用await for async类吗 我想等待我的消息。这是一个回调方法。返回NewStocktakin值后,我必须在第行下面继续。有可能吗?因为我的存货ID返回0 public int NewStocktaking(Stocktaking stocktaking, String userId, Constants.StocktakingType stocktakingType) { new NewStocktakingService(coun
public int NewStocktaking(Stocktaking stocktaking, String userId, Constants.StocktakingType stocktakingType) {
new NewStocktakingService(count -> count).
execute(
String.valueOf(stocktaking.getName()),
String.valueOf(stocktaking.getRelatedId()),
String.valueOf(userId),
String.valueOf(stocktakingType)
);
return 0;
}
还有我的脸
public interface AsyncResponseNewStocktaking {
int processFinish(int count);
}
还有我的异步类
public class NewStocktakingService extends AsyncTask<String, Void, ResponseModel> {
AsyncResponseNewStocktaking delegate = null;
NewStocktakingService(AsyncResponseNewStocktaking delegate) {
this.delegate = delegate;
}
@Override
protected ResponseModel doInBackground(String... parameters) {
RequestHandler requestHandler = new RequestHandler();
ResponseModel responseModel = requestHandler.getRequestGet(UHFApplication.getInstance().apiUrl + "/api/MobileService/NewStocktaking?" +
"stocktakingName=" + URLEncoder.encode(parameters[0]) +
"&relatedID=" + parameters[1] +
"&userID=" + parameters[2] +
"&stocktakingType=" + parameters[3]);
return responseModel;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(ResponseModel responseModel) {
super.onPostExecute(responseModel);
if (HttpURLConnection.HTTP_OK == responseModel.httpStatus) {
int count = Integer.parseInt(responseModel.responseString);
delegate.processFinish(count);
} else {
delegate.processFinish(0);
}
}
}
公共类NewStocktakingService扩展了异步任务{
AsyncResponseNewStocktaking委托=null;
NewStocktakingService(AsyncResponseNewStockTakingDelegate){
this.delegate=委托;
}
@凌驾
受保护的响应模式doInBackground(字符串…参数){
RequestHandler RequestHandler=新的RequestHandler();
ResponseModel ResponseModel=requestHandler.getRequestGet(UHFApplication.getInstance().apiUrl+“/api/MobileService/NewStocktaking?”+
“stocktakingName=“+URLEncoder.encode(参数[0])+
“&relatedID=“+参数[1]+
“&userID=“+参数[2]+
“&stocktakingType=“+参数[3]);
返回响应模型;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的void onPostExecute(ResponseModel ResponseModel){
super.onPostExecute(responseModel);
if(HttpURLConnection.HTTP_OK==responseModel.httpStatus){
int count=Integer.parseInt(responseModel.responseString);
委托.进程完成(计数);
}否则{
委托.processFinish(0);
}
}
}
如果需要AsyncTask
类的值。您可以使用.get()
方法等待
因此,您的最终代码应该如下所示:
public int NewStocktaking(Stocktaking stocktaking, String userId, Constants.StocktakingType stocktakingType) {
return new NewStocktakingService(count -> count).
execute(
String.valueOf(stocktaking.getName()),
String.valueOf(stocktaking.getRelatedId()),
String.valueOf(userId),
String.valueOf(stocktakingType)
).get();
}
但是,这将要求您使AsynTask返回
整数
。检查代码时,您无论如何都不会使用ResponeModel
的其他属性,因此可以使onBackground()
返回整数 如果需要AsyncTask
类的值。您可以使用.get()
方法等待
因此,您的最终代码应该如下所示:
public int NewStocktaking(Stocktaking stocktaking, String userId, Constants.StocktakingType stocktakingType) {
return new NewStocktakingService(count -> count).
execute(
String.valueOf(stocktaking.getName()),
String.valueOf(stocktaking.getRelatedId()),
String.valueOf(userId),
String.valueOf(stocktakingType)
).get();
}
但是,这将要求您使AsynTask返回
整数
。检查代码时,您无论如何都不会使用ResponeModel
的其他属性,因此可以使onBackground()
返回整数 哦,很好,非常感谢:)哦,很好,非常感谢:)