我在我的项目中添加了异步库,并且已经检查了,我不知道为什么代码流不会进入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); }
我在代码“Log.v(”AsyncTask“,”doInBackground“)中有一个日志语句;
但它不会出现在记录器中的Log.v(“AsyncTask”,“doInBackground”);
解决方法
你应该执行你的任务
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
因为在较低版本的Android中,所有AsyncTasks都在单个后台线程中执行.所以新任务可能会等待,直到其他任务工作.
在Android的较低版本(实际上在HONEYCOMB之前),您无法在执行器上执行AsyncTask.
将您的代码更改为
public void executeAsyncTask() { AsyncTask<Void,String> task = new AsyncTask<Void,"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(); } }