AsyncTask - Does not run twice

0

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

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

I've researched 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 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?

    
asked by anonymous 03.01.2018 / 00:52

1 answer

2

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

  • The AsyncTask class must be loaded into the UI.
  • The task instance must be created in the UI.
  • execute(Params...) must be invoked in the UI.
  • Do not call methods onPreExecute() , onPostExecute() and doInBackground()
  • The task can be executed only once
  • Since you are setting BackGroundWorkerLO bkwlo = new BackGroundWorkerLO(this); out of the UI segment, when you call execute , android will check if you have already run this code. If so, it will throw a exception .

    To avoid this, you should create a new instance with each run.

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