Asynctask - Does not run twice

Asked

Viewed 98 times

0

I am performing a process in my App, where in Mainactivity, I press a button, then it loads a JSON in another Activity that brings all the information correctly.. However, when I close the screen and try to view the data listing again, it shows the following error:

Cannot execute task: the task has already been executed (a task can be executed only once)

I have already searched and analyzed in several places that I can not run twice, tried to instantiate again, but always return the same error. Below the code:

Function in the Mainactivity

BackGroundWorkerLO bkwlo = new BackGroundWorkerLO(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    btOve = (ImageButton) findViewById(R.id.btOve);
    btOve.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           bkwlo.execute();
        }
    });

Asynchronous class:

public class BackGroundWorkerLO extends AsyncTask<String, Void, String> {

Context context;

public BackGroundWorkerLO(Context context){
    this.context = context;
}

@Override
protected String doInBackground(String... strings) {
    String tipo = "lista";
    String url_ovelha = "http://blbiandoge.000webhostapp.com/app/file.json";

    if(tipo.equals("lista")) {
        try {
            URL url = new URL(url_ovelha);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
            String result="";
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;

        } catch (IOException e) {
            Log.i("ErroListaO", "Erro na lista");
            e.printStackTrace();
        }
    }
    return null;
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    Intent intentLO = new Intent(context, ListaOveActivity.class);
    intentLO.putExtra("JSON", s);
    context.startActivity(intentLO);
}

Can anyone help ?

1 answer

2


According to the documentation of Asynctask, there are some rules that need to be followed:

  1. Asynctask class must be loaded into the UI.
  2. The task instance should be created in the UI.
  3. execute(Params...) must be invoked in the UI.
  4. Do not call the methods onPreExecute(), onPostExecute() and doInBackground()
  5. The task can be executed only once

How are you defining BackGroundWorkerLO bkwlo = new BackGroundWorkerLO(this); outside the UI segment, when you call the execute, android will check if you have ever run this code. If so, it will release a exception.

To avoid this, you must create a new instance at each run.

btOve = (ImageButton) findViewById(R.id.btOve);
btOve.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       new BackGroundWorkerLO(view.getContext()).execute();
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.