英文:
My AsyncTask in android not executing do in background , Why?
问题
// AsyncTask
package com.test.testing;
// 导入语句
public class Update_Score extends AsyncTask<Void, Void, Void> {
String result = "vvv",
name,
score,
lastTest,
maximum;
ProgressBar loading;
TextView text;
Context context;
@Override
protected Void doInBackground(Void... voids) {
String strUrl = "http://test.tk/updatesomething.php";
while (result == "vvv") {
try {
URL url = new URL(strUrl);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
// 输出
OutputStream output = httpConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
String post_data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&"
+ URLEncoder.encode("score", "UTF-8") + "=" + URLEncoder.encode(score, "UTF-8") + "&"
+ URLEncoder.encode("lastTest", "UTF-8") + "=" + URLEncoder.encode(lastTest, "UTF-8");
writer.write(post_data);
writer.flush();
writer.close();
output.close();
// 输入
InputStream input = httpConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input, "iso-8859-1"));
result = "";
String line = "";
while ((line = reader.readLine()) != null) {
result += line;
}
reader.close();
input.close();
httpConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
new AlertDialog.Builder(context).setMessage("updateEnd").create().show();
return null;
}
}
// 调用 AsyncTask
package com.test.testing;
// 导入语句
public class Final_Score extends AppCompatActivity {
public TextView score;
public ProgressBar loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final__score);
initVar();
String totalMark = getIntent().getStringExtra("TOTAL_MARKS"),
name = getIntent().getStringExtra("NAME"),
lastTest = getIntent().getStringExtra("TESTNUM"),
maximum = getIntent().getStringExtra("MAX");
boolean forced = getIntent().getBooleanExtra("FORCED", false);
if (forced) {
new AlertDialog.Builder(this).setMessage("Time Up").setTitle("").create().show();
}
score.setText(totalMark);
Update_Score update = new Update_Score(name, totalMark, lastTest, loading, this, score, maximum);
update.execute();
}
}
英文:
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.
// AsyncTask
package com.test.testing;
//imports
public class Update_Score extends AsyncTask<Void, Void, Void> {
String result="vvv",
name,
score,
lastTest,
maximum;
ProgressBar loading;
TextView text;
Context context;
@Override
protected Void doInBackground(Void... voids) {
String strUrl="http://test.tk/updatesomething.php";
while (result=="vvv") {
try {
URL url = new URL(strUrl);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
//output
OutputStream output = httpConnection.getOutputStream();
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(output,"UTF-8"));
String post_data= URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
+URLEncoder.encode("score","UTF-8")+"="+URLEncoder.encode(score,"UTF-8")+"&"
+URLEncoder.encode("lastTest","UTF-8")+"="+URLEncoder.encode(lastTest,"UTF-8");
writer.write(post_data);
writer.flush();
writer.close();
output.close();
//input
InputStream input = httpConnection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(input,"iso-8859-1"));
result="";
String line="";
while ((line=reader.readLine())!=null){
result += line;
}
reader.close();
input.close();
httpConnection.disconnect();
} catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) {
e.printStackTrace();
}
}
new AlertDialog.Builder(context).setMessage("updateEnd").create().show();
return null;
}
}
// Calling AsyncTask
package com.test.testing;
//imports
public class Final_Score extends AppCompatActivity {
public TextView score;
public ProgressBar loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final__score);
initVar();
String totalMark =getIntent().getStringExtra("TOTAL_MARKS"),
name =getIntent().getStringExtra("NAME") ,
lastTest =getIntent().getStringExtra("TESTNUM"),
maximum =getIntent().getStringExtra("MAX");
boolean forced=getIntent().getBooleanExtra("FORCED",false);
if(forced){
new AlertDialog.Builder(this).setMessage("Time Up").setTitle("").create().show();
}
score.setText(totalMark);
Update_Score update=new Update_Score(name,totalMark,lastTest,loading,this,score,maximum);
update.execute();
}
}
Thanks in advance.
答案1
得分: 1
我遇到的问题是,我之前提到的任务仍在运行,即使我调用了AsyncTask.cancel(true)
也无法取消。
这是一个循环任务,会一直循环直到达到特定的时间(比如11:17)。
while(!mainActivity.timeNotEnds){
//一些操作在这里
if (time==previouslySpecifiedTime){
break;
}
}
所以我在主活动中创建了一个布尔变量,并将任务中的代码改为
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 .
while(!mainActivity.timeNotEnds){
//some stuff here
if (time==previouslySpecifiedTime){
break;
}
}
so instead i made a boolean in the main activity and set the code in that task like
while(!mainActivity.timeNotEnds)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论