简体   繁体   English

我在android中的AsyncTask没有在后台执行,为什么?

[英]My AsyncTask in android not executing do in background , Why?

My AsyncTask not executing doInBackground , preExecute is running i tested it with a Toast .我的 AsyncTask 没有执行 doInBackground , preExecute 正在运行,我用 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.提前致谢。

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 .我遇到了我所说的正在运行的任务也没有取消的问题,即使我调用AsyncTask.cancel(true)它是一个循环直到特定时间(如 11:17)到达的任务。

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)所以我在主活动中创建了一个布尔值,并在该任务中设置代码,如while(!mainActivity.timeNotEnds)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM