有人知道我为什么会在标题中出现这个错误吗?我真的很难从dialogflow api得到回复来制作我的聊天机器人。这是我的密码:
主活动.java
public class MainActivity extends AppCompatActivity {
private final int USER = 0;
private final int BOT = 1;
SessionsClient sessionsClient;
SessionName sessionName;
UUID uuid;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uuid = UUID.randomUUID();
editText = findViewById(R.id.edittext_chatbox);
FloatingActionButton btn = findViewById(R.id.send_button);
btn.setOnClickListener(v -> sendMsg());
initBob();
}
private void initBob() {
try {
InputStream stream = getResources().openRawResource(R.raw.bob_credentials);
GoogleCredentials cred = GoogleCredentials.fromStream(stream);
String pID = ((ServiceAccountCredentials) cred).getProjectId();
SessionsSettings.Builder settingBuilder = SessionsSettings.newBuilder();
SessionsSettings sessionsSettings = settingBuilder.setCredentialsProvider(FixedCredentialsProvider.create(cred)).build();
sessionsClient = SessionsClient.create(sessionsSettings);
sessionName = SessionName.of(pID, uuid.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
private void sendMsg() {
String msg = editText.getText().toString();
if (msg.trim().isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter a message", Toast.LENGTH_LONG).show();
} else {
showTextView(msg, USER);
editText.setText("");
QueryInput queryInput = QueryInput.newBuilder().setText(TextInput.newBuilder().setText(msg).setLanguageCode("en-US")).build();
new Request(MainActivity.this, sessionName, sessionsClient, queryInput).execute();
}
}
private void showTextView(String message, int type) {
LinearLayout chatLayout = findViewById(R.id.chat_layout);
FrameLayout layout;
switch (type) {
case USER:
layout = getUserLayout();
break;
default:
layout = getBotLayout();
break;
}
layout.setFocusableInTouchMode(true);
chatLayout.addView(layout);
TextView tv = layout.findViewById(R.id.chat_msg);
tv.setText(message);
layout.requestFocus();
editText.requestFocus();
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa",
Locale.ENGLISH);
String time = dateFormat.format(date);
TextView timeTextView = layout.findViewById(R.id.message_time);
timeTextView.setText(time);
}
public void callBack(DetectIntentResponse response) {
if (response != null){
String botReply = response.getQueryResult().getFulfillmentText();
showTextView(botReply, BOT);
}
else{
showTextView("wot", BOT);
}
}
FrameLayout getUserLayout() {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
return (FrameLayout) inflater.inflate(R.layout.human_message, null);
}
FrameLayout getBotLayout() {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
return (FrameLayout) inflater.inflate(R.layout.bot_message, null);
}
}
请求.java
@SuppressWarnings("deprecation")
public class Request extends AsyncTask<Void,Void, DetectIntentResponse> {
Activity activity;
SessionName session;
SessionsClient sessionsClient;
QueryInput queryInput;
Request(Activity activity, SessionName session, SessionsClient sessionsClient, QueryInput queryInput) {
this.activity = activity;
this.session = session;
this.sessionsClient = sessionsClient;
this.queryInput = queryInput;
}
@Override
protected DetectIntentResponse doInBackground(Void... voids) {
try{
DetectIntentRequest detectIntentRequest = DetectIntentRequest.newBuilder()
.setSession(session.toString())
.setQueryInput(queryInput)
.build();
return sessionsClient.detectIntent(detectIntentRequest);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(DetectIntentResponse response){
((MainActivity) activity).callBack(response);
}
}
它看起来像googlecredentials cred=googlecredentials.fromstream(stream);而随机uuid()就是问题所在?我不确定。
暂无答案!
目前还没有任何答案,快来回答吧!