简体   繁体   中英

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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