LKM*_*LKM 24 android android-asynctask
我在我的项目中添加了异步库并已经检查过,我不知道为什么代码流不会进入asynctask
码
public void doMysql()
{
Log.v("doMysql", "accessed");
new AsyncTask<Void, Void, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.e("AsyncTask", "onPreExecute");
}
@Override
protected String doInBackground(Void... params) {
Log.v("AsyncTask", "doInBackground");
String msg = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://172.16.100.172:52273/mysql");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myday", Integer.toString(day_picker.getYear()) +
addZero(day_picker.getMonth() + 1) +
addZero(day_picker.getDayOfMonth())));
nameValuePairs.add(new BasicNameValuePair("mystar", changeStar(day_picker.getMonth() + 1, day_picker.getDayOfMonth())));
nameValuePairs.add(new BasicNameValuePair("mybt", changeBloodType(blood_picker.getValue())));
nameValuePairs.add(new BasicNameValuePair("mynum", "" + myPhone.getText()));
nameValuePairs.add(new BasicNameValuePair("yournum", "" + partnerPhone.getText()));
nameValuePairs.add(new BasicNameValuePair("myregID", regid));
try {
Log.v("setEntity", "before");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.v("setEntity", "after");
} catch (UnsupportedEncodingException e1) {
Log.v("UnsupportedEncodingException", "");
e1.printStackTrace();
}
//172.16.101.28
try {
Log.v("post", "before");
HttpResponse httpresponse = httpclient.execute(httppost);
Log.v("post", "after");
Log.v("HttpResponse ",httpresponse.getEntity().toString());
} catch (ClientProtocolException e) {
Log.v("ClientProtocolException", "ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
Log.v("IOException", "IOException");
e.printStackTrace();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
Log.v("AsyncTask", "onPostExecute");
}
}.execute(null, null, null);
}
Run Code Online (Sandbox Code Playgroud)
我在代码'Log.v("AsyncTask","doInBackground")中有一个日志语句;'
但它没有出现在记录器'Log.v("AsyncTask","doInBackground")中;'
Ser*_*kov 56
您应该使用执行程序执行您的任务
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Run Code Online (Sandbox Code Playgroud)
因为在较低版本的Android中,所有AsyncTasks都是在单个后台线程上执行的.所以新任务可能正在等待,直到其他任务正常工作.
在Android的较低版本中(实际上在pre-HONEYCOMB上),您无法在执行程序上执行AsyncTask.
将您的代码更改为
public void executeAsyncTask()
{
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.e("AsyncTask", "onPreExecute");
}
@Override
protected String doInBackground(Void... params) {
Log.v("AsyncTask", "doInBackground");
String msg = null;
// some calculation logic of msg variable
return msg;
}
@Override
protected void onPostExecute(String msg) {
Log.v("AsyncTask", "onPostExecute");
}
};
if(Build.VERSION.SDK_INT >= 11/*HONEYCOMB*/) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
task.execute();
}
}
Run Code Online (Sandbox Code Playgroud)
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Run Code Online (Sandbox Code Playgroud)
我已经这样做了,但问题仍然存在,问题是我AsyncTask用来运行长时间运行的后台任务,所以ThreadPoolExecutor工作线程已经被占用了。
在我的情况下AsyncTask.THREAD_POOL_EXECUTOR(使用调试器获取您的执行程序属性)具有 ff 属性:
maximumPoolSize = 3
largetsPoolSize = 2
corePoolSize =2
Run Code Online (Sandbox Code Playgroud)
因此,禁用长时间运行的 asyncTasks 之一使我的 doInBackground 代码运行,但这不是正确的解决方案。
正确的解决方案是我将长时间运行的任务移到自定义线程中,而不是使用AsyncTask.
我处于同样的境地.
通过添加日志语句,我意识到第一个异步任务仍在运行...从而阻止执行另一个异步大头钉.它的doInBackground方法运行了一个无限循环,所以我只需要改变条件来考虑所有可能的"破解"情况......
引用Android文档进行异步任务
首次引入时,AsyncTasks在单个后台线程上串行执行。从DONUT开始,它已更改为线程池,允许多个任务并行运行。从HONEYCOMB开始,任务在单个线程上执行,以避免并行执行引起的常见应用程序错误。
如果您确实希望并行执行,则可以使用THREAD_POOL_EXECUTOR调用executeOnExecutor(java.util.concurrent.Executor,Object [])。
使用以下代码段触发AsyncTask:
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
asyncTaskInstance.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
else
pdfPageChanger.execute(params);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28922 次 |
| 最近记录: |