我的Android中的AsyncTask未执行后台操作,为什么?

huangapple go评论90阅读模式
英文:

My AsyncTask in android not executing do in background , Why?

问题

  1. // AsyncTask
  2. package com.test.testing;
  3. // 导入语句
  4. public class Update_Score extends AsyncTask<Void, Void, Void> {
  5. String result = "vvv",
  6. name,
  7. score,
  8. lastTest,
  9. maximum;
  10. ProgressBar loading;
  11. TextView text;
  12. Context context;
  13. @Override
  14. protected Void doInBackground(Void... voids) {
  15. String strUrl = "http://test.tk/updatesomething.php";
  16. while (result == "vvv") {
  17. try {
  18. URL url = new URL(strUrl);
  19. HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
  20. httpConnection.setRequestMethod("POST");
  21. httpConnection.setDoOutput(true);
  22. httpConnection.setDoInput(true);
  23. // 输出
  24. OutputStream output = httpConnection.getOutputStream();
  25. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
  26. String post_data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&"
  27. + URLEncoder.encode("score", "UTF-8") + "=" + URLEncoder.encode(score, "UTF-8") + "&"
  28. + URLEncoder.encode("lastTest", "UTF-8") + "=" + URLEncoder.encode(lastTest, "UTF-8");
  29. writer.write(post_data);
  30. writer.flush();
  31. writer.close();
  32. output.close();
  33. // 输入
  34. InputStream input = httpConnection.getInputStream();
  35. BufferedReader reader = new BufferedReader(new InputStreamReader(input, "iso-8859-1"));
  36. result = "";
  37. String line = "";
  38. while ((line = reader.readLine()) != null) {
  39. result += line;
  40. }
  41. reader.close();
  42. input.close();
  43. httpConnection.disconnect();
  44. } catch (MalformedURLException e) {
  45. e.printStackTrace();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. new AlertDialog.Builder(context).setMessage("updateEnd").create().show();
  51. return null;
  52. }
  53. }
  54. // 调用 AsyncTask
  55. package com.test.testing;
  56. // 导入语句
  57. public class Final_Score extends AppCompatActivity {
  58. public TextView score;
  59. public ProgressBar loading;
  60. @Override
  61. protected void onCreate(Bundle savedInstanceState) {
  62. super.onCreate(savedInstanceState);
  63. setContentView(R.layout.activity_final__score);
  64. initVar();
  65. String totalMark = getIntent().getStringExtra("TOTAL_MARKS"),
  66. name = getIntent().getStringExtra("NAME"),
  67. lastTest = getIntent().getStringExtra("TESTNUM"),
  68. maximum = getIntent().getStringExtra("MAX");
  69. boolean forced = getIntent().getBooleanExtra("FORCED", false);
  70. if (forced) {
  71. new AlertDialog.Builder(this).setMessage("Time Up").setTitle("").create().show();
  72. }
  73. score.setText(totalMark);
  74. Update_Score update = new Update_Score(name, totalMark, lastTest, loading, this, score, maximum);
  75. update.execute();
  76. }
  77. }
英文:

My AsyncTask not executing doInBackground , preExecute is running i tested it with a Toast .

I am also running another asynctask but when i starts this one i cancels that one still it wont works.

It's not throwing any errors still this happens to me.

Here's my code any help will be appreciated .

some codes are been cutted off . I am just showing the important parts.

And sorry if there's any mistake in my english.

  1. // AsyncTask
  2. package com.test.testing;
  3. //imports
  4. public class Update_Score extends AsyncTask&lt;Void, Void, Void&gt; {
  5. String result=&quot;vvv&quot;,
  6. name,
  7. score,
  8. lastTest,
  9. maximum;
  10. ProgressBar loading;
  11. TextView text;
  12. Context context;
  13. @Override
  14. protected Void doInBackground(Void... voids) {
  15. String strUrl=&quot;http://test.tk/updatesomething.php&quot;;
  16. while (result==&quot;vvv&quot;) {
  17. try {
  18. URL url = new URL(strUrl);
  19. HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
  20. httpConnection.setRequestMethod(&quot;POST&quot;);
  21. httpConnection.setDoOutput(true);
  22. httpConnection.setDoInput(true);
  23. //output
  24. OutputStream output = httpConnection.getOutputStream();
  25. BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(output,&quot;UTF-8&quot;));
  26. String post_data= URLEncoder.encode(&quot;name&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(name,&quot;UTF-8&quot;)+&quot;&amp;&quot;
  27. +URLEncoder.encode(&quot;score&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(score,&quot;UTF-8&quot;)+&quot;&amp;&quot;
  28. +URLEncoder.encode(&quot;lastTest&quot;,&quot;UTF-8&quot;)+&quot;=&quot;+URLEncoder.encode(lastTest,&quot;UTF-8&quot;);
  29. writer.write(post_data);
  30. writer.flush();
  31. writer.close();
  32. output.close();
  33. //input
  34. InputStream input = httpConnection.getInputStream();
  35. BufferedReader reader=new BufferedReader(new InputStreamReader(input,&quot;iso-8859-1&quot;));
  36. result=&quot;&quot;;
  37. String line=&quot;&quot;;
  38. while ((line=reader.readLine())!=null){
  39. result += line;
  40. }
  41. reader.close();
  42. input.close();
  43. httpConnection.disconnect();
  44. } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. new AlertDialog.Builder(context).setMessage(&quot;updateEnd&quot;).create().show();
  49. return null;
  50. }
  51. }
  52. // Calling AsyncTask
  53. package com.test.testing;
  54. //imports
  55. public class Final_Score extends AppCompatActivity {
  56. public TextView score;
  57. public ProgressBar loading;
  58. @Override
  59. protected void onCreate(Bundle savedInstanceState) {
  60. super.onCreate(savedInstanceState);
  61. setContentView(R.layout.activity_final__score);
  62. initVar();
  63. String totalMark =getIntent().getStringExtra(&quot;TOTAL_MARKS&quot;),
  64. name =getIntent().getStringExtra(&quot;NAME&quot;) ,
  65. lastTest =getIntent().getStringExtra(&quot;TESTNUM&quot;),
  66. maximum =getIntent().getStringExtra(&quot;MAX&quot;);
  67. boolean forced=getIntent().getBooleanExtra(&quot;FORCED&quot;,false);
  68. if(forced){
  69. new AlertDialog.Builder(this).setMessage(&quot;Time Up&quot;).setTitle(&quot;&quot;).create().show();
  70. }
  71. score.setText(totalMark);
  72. Update_Score update=new Update_Score(name,totalMark,lastTest,loading,this,score,maximum);
  73. update.execute();
  74. }
  75. }

Thanks in advance.

答案1

得分: 1

我遇到的问题是,我之前提到的任务仍在运行,即使我调用了AsyncTask.cancel(true)也无法取消。
这是一个循环任务,会一直循环直到达到特定的时间(比如11:17)。

  1. while(!mainActivity.timeNotEnds){
  2. //一些操作在这里
  3. if (time==previouslySpecifiedTime){
  4. break;
  5. }
  6. }

所以我在主活动中创建了一个布尔变量,并将任务中的代码改为

  1. while(!mainActivity.timeNotEnds)
英文:

I got the problem the task which i said was running also doesn't cancels even i calls AsyncTask.cancel(true)
it was a task whcih loops until a specific time (like 11:17) is reached .

  1. while(!mainActivity.timeNotEnds){
  2. //some stuff here
  3. if (time==previouslySpecifiedTime){
  4. break;
  5. }

}

so instead i made a boolean in the main activity and set the code in that task like
while(!mainActivity.timeNotEnds)

huangapple
  • 本文由 发表于 2020年10月3日 15:22:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64181729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定