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?