As I understand it, the problem is in blocking the thread, but if I add a separate thread, the call is simply ignored, help me run it in a separate thread
private void showDialog(){
int random = ThreadLocalRandom.current().nextInt(0, 28);
String[] randomPhrase = getResources().getStringArray(R.array.random_phrase);
String[] randomPhraseErr = getResources().getStringArray(R.array.random_phrase_err);
Dialog dialogAI = new Dialog(this, R.style.DialogStyle);
dialogAI.setContentView(R.layout.custom_dialog_ai);
dialogAI.getWindow().setBackgroundDrawableResource(R.drawable.bg_cyrcle_window);
TypeWriterView typeWriterView = dialogAI.findViewById(R.id.typeWriter_openai);
AppCompatButton appCompatButton = dialogAI.findViewById(R.id.btn_generate);
SearchView searchView_ai = dialogAI.findViewById(R.id.searchView_ai);
typeWriterView.setCharacterDelay(30);
typeWriterView.animateText("ИИ:\n" + randomPhrase[random]);
appCompatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String searchViewText = searchView_ai.getQuery().toString();
if (searchViewText.isEmpty()) {
int random_ = ThreadLocalRandom.current().nextInt(0, 10);
typeWriterView.animateText("ИИ:\n" + randomPhraseErr[random_]);
}
else
{
Request request = new Request.Builder().url(getURL).build();
clientOpenAI.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
runOnUiThread(new Runnable() {
public void run() {
try {
typeWriterView.animateText("ИИ: " + response.body().string());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
});
}
}
});
dialogAI.show();
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... log-layout
Приложение сбоя перед новой функцией вызова в макете диалога ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Достижение плавного прокрутки в EditText с помощью нижнего утечка / диалога / диалога
Anonymous » » в форуме Android - 0 Ответы
- 1 Просмотры
-
Последнее сообщение Anonymous
-