Я использую одну кнопку для выполнения двух задач.Single button multiple action
- Watson Разговор.
- Watson текст в речь.
Мой код выполняется только в том случае, если у моего TextView есть текстовое имя (строка), но текст в речь воспроизводит последний ответ от ответа, даже если новый ответ отклика обновляется на дисплее TextView на моем пользовательском интерфейсе телефона. Продолжение этого здесь Race condition with UI thread issue.
Также я узнал, если я держу TextView пустой я получаю ошибку: это код здесь:
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("xxxxxxx", newMessage).enqueue(new ServiceCallback<MessageResponse>() {
@Override
public void onResponse(MessageResponse response) {
context = response.getContext();
textResponse = response.getText().get(0);
reply.setText(textResponse);
System.out.println(textResponse);
}
@Override
public void onFailure(Exception e) {
}
});
return textResponse;
}
}
//
private class WatsonTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(final String... textToSpeak) {
/* runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(" ");
}
});*/
TextToSpeech textToSpeech = initTextToSpeechService();
streamPlayer = new StreamPlayer();
streamPlayer.playStream(textToSpeech.synthesize(textToSpeak[0], Voice.EN_LISA).execute());
return "Text to Speech Done";
}
/*@Override protected void onPostExecute(String result) {
textView.setText("");
}*/
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Register the UI controls.
input = (EditText) findViewById(R.id.input);
send = (ImageButton) findViewById(R.id.send);
textView = (TextView) findViewById(R.id.textView);
reply = (TextView) findViewById(R.id.reply);
play = (ImageButton) findViewById(R.id.play);
new ConversationTask().execute("");
//Button function
send.setOnClickListener(action3);
}
//five actions on button click
public void action5() {
String textResponse = new String();
System.out.println("Text to Speech:" + reply.getText());
//textView.setText("");
WatsonTask task = new WatsonTask();
task.execute(String.valueOf(reply.getText()));
//new WatsonTask().execute(reply.getText().toString());
}
View.OnClickListener action3 = new View.OnClickListener() {
public void onClick(View v) {
//action here//
new ConversationTask().execute(input.getText().toString());
action5();
}
};
}
Пожалуйста, помогите.
Предоставлять код и ошибки не на фотографиях. вы можете опубликовать их отформатированные здесь. – XtremeBaumer
Добавил код выше. Пожалуйста, проверьте –
. Вы пытались переместить логику action5() в onResponse внутри asyncTask? –