I've got an AsyncTask class
that will do a HttpGet-request
. I want to do something after this AsyncTask
is done, but from within my MainActivity
.
Here is my TaskGetAPI class
:
public class TaskGetAPI extends AsyncTask<String, Void, String>
{
private TextView output;
private Controller controller;
public TaskGetAPI(TextView output){
this.output = output;
}
@Override
protected String doInBackground(String... urls){
String response = "";
for(String url : urls){
HttpGet get = new HttpGet(url);
try{
// Send the GET-request
HttpResponse execute = MainActivity.HttpClient.execute(get);
// Get the response of the GET-request
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while((s = buffer.readLine()) != null)
response += s;
content.close();
buffer.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result){
if(!Config.LOCALHOST)
output.setText(result);
else
controller = Controller.fromJson(result);
}
public Controller getController(){
return controller;
}
And here is the method from my MainActivity
where I use this class:
private void sendGetRequest(){
...
// Web API GET-request
if(!get_url.equals("") && get_url != null){
TaskGetAPI task = new TaskGetAPI(output);
task.execute(new String[] { get_url });
// TODO: When AsyncTask is done, do:
controller = task.getController();
Log.i("CONTROLLER", controller.toString());
}
}
As you can see I set the Controller
that I use later on in the onPostExecute-method
of the AsyncTask
.
Since this counters the entire purpose of Async tasks
I first thought of removing the extends AsyncTask
and just make a regular class & method of my HttpGet
, but then I get a android.os.NetworkOnMainThreadException
, meaning I need to use an AsyncTask
(or something similar) to use HttpGet
from within a different thread
than my MainThread
.
So, does anyone know what I should put at the // TODO
?
I did try adding a boolean field (isDone)
to the TaskGetAPI
class with a getter
and then use:
while(true){
if(task.isDone()){
Controller controller = task.getController();
Log.i("CONTROLLER", controller.toString());
}
}
But then the following steps occur:
doInBackground
of theTaskGetAPI
class is completely done.- Now we are stuck in this
while(true)-loop
..
and onPostExecute
which sets the isDone
to true
is never called.