Java 我应该在我的应用程序上使用.executeOnExecutor吗?
我在Java 我应该在我的应用程序上使用.executeOnExecutor吗?,java,android,android-asynctask,Java,Android,Android Asynctask,我在Android上做了一个restapi,其中我有多个AsyncTask(一个用于HTTPGet,一个用于HTTPPost),我对它们没有任何问题,但当我试图在使用HTTPDelete的地方执行AsyncTask时,它不执行doInBackground()方法 在中搜索,我找到了这个解决方案:但我对使用.executeOnExecutor不安全,因为我在官方文档中搜索:它说这可能是问题 例如,如果这些任务用于修改任何公共状态(例如,由于单击按钮而写入文件),则无法保证修改的顺序 我正在使用我的
Android
上做了一个restapi
,其中我有多个AsyncTask
(一个用于HTTPGet
,一个用于HTTPPost
),我对它们没有任何问题,但当我试图在使用HTTPDelete
的地方执行AsyncTask
时,它不执行doInBackground()
方法
在中搜索,我找到了这个解决方案:但我对使用.executeOnExecutor
不安全,因为我在官方文档中搜索:它说这可能是问题
例如,如果这些任务用于修改任何公共状态(例如,由于单击按钮而写入文件),则无法保证修改的顺序
我正在使用我的GET
、POST
和DELETE
方法修改我的数据库(我知道它不是文件,但我想它可能会给我带来同样的问题)
根据这一点,我不确定如何执行此.doInBackground()
方法,因为我不希望将来出现问题
我应该用什么
这是我的异步任务
:
class DeleteCar extends AsyncTask<Integer, Integer, Void> {
protected void onPreExecute(){
}
protected Void doInBackground(Integer... id) {
try{
String url = "http://IP of my computer/project/cars/" + id[0].intValue();
HttpClient httpClient = new DefaultHttpClient();
HttpDelete method= new HttpDelete(url);
method.setHeader("content-type", "application/json");
HttpResponse response = httpClient.execute(method);
HttpEntity httpEntity = response.getEntity();
}catch(Exception ex){
Log.e("ServicioRest",ex.toString());
}
return null;
}
protected void onProgressUpdate(){
}
protected void onPostExecute(){
}
}
其中idCar
为Integer-idCar=new-Integer(id)
和id
这是一个int
注意:它在onPreExecute()
方法中输入,但不是在doInBackground()方法中输入
public class DeleteCar extends AsyncTask<Integer,Integer,Void> {
@Override
protected Void doInBackground(Integer... params) {
//logging to show that it is working
Log.d("result:", String.valueOf(params[0].intValue()));
return null;
}
它完美地工作/执行doInBackground方法
我的建议是改型。以下是了解改装的链接:
1.
2.这里有一个比较:
3.以下是一个基本教程:
我一直在使用改装。我得到了更快的回应。如果您需要更多的帮助来使用改装,请让我知道。谢谢执行方法上的idCar是整数对象吗?@MdOmarFaroqueAnik是的。这是一个intidcar=5代码>int是基本的。整数是一个对象。如果新整数(5)有效,请尝试这样使用它。@MdOmarFaroqueAnik哦,对不起。我还以为您在doInBackground()
方法中引用了id
idCar
它是Integer idCar=new Integer(id)
其中id
它是一个int
。请改型我的朋友。它真的更快。我给了你下面的链接。谢谢
public class DeleteCar extends AsyncTask<Integer,Integer,Void> {
@Override
protected Void doInBackground(Integer... params) {
//logging to show that it is working
Log.d("result:", String.valueOf(params[0].intValue()));
return null;
}
new DeleteCar().execute(new Integer(10));