This is the code i am working on. Here I cant update the UI until myOnResponse
is finished.Because we are doing a doInBackgrnd
, so my textresponse
is empty. And Since onPostExecute
is happening right after.
For his I think PublicProgres should help. How to Call PublishProgress at AsyncTask ?
private class ConversationTask extends AsyncTask<String, Void, String> {
String textResponse = new String();
@Override
protected String doInBackground(String... params) {
System.out.println("in doInBackground");
MessageRequest newMessage = new MessageRequest.Builder().inputText(params[0]).context(context).build();
// async
GLS_service.message("xxxxxxxxx", newMessage).enqueue(new ServiceCallback<MessageResponse>() {
@Override
public void onResponse(MessageResponse response) {
context = response.getContext();
textResponse = response.getText().get(0);
action5(textResponse);
System.out.println(textResponse);
}
@Override
public void onFailure(Exception e) {
}
});
return textResponse;
}@Override protected void onPostExecute(String result) {
reply.setText(textResponse);
}
}
Please help.
I don't think that you have to use
AsyncTask
.You can do something like this :
YourTask.java
And now how to use it :
SomeActivity.java
Nevertheless if you want to do this action in
Asynktask
just try thisHope it helps