IBM Watson Assistant:对话在 "Try it out" 应用程序中工作但不在 Android 应用程序中工作
IBM Watson Assistant: Dialog working in "Try it out" but not in Android app
1这里是完整的来源 code.I 已尝试但无法在 logcat 中显示 json 响应。我如何才能从 ibm watson 获得完整的响应以及如何在我的聊天机器人中显示它。
public class MainActivity extends AppCompatActivity {
TextView conversation;
EditText userInput;
Button btnSend;
JSONObject jsonObject;
Object object;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conversation = (TextView)findViewById(R.id.conversation);
userInput = (EditText)findViewById(R.id.user_input);
btnSend=(Button) findViewById(R.id.sendbutton);
final ConversationService myConversationService =
new ConversationService(
"2017-05-26",
getString(R.string.username),
getString(R.string.password)
);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "working", Toast.LENGTH_SHORT).show();
final String inputText = userInput.getText().toString();
conversation.append(
Html.fromHtml("<p><b>You:</b> " + inputText + "</p>")
);
userInput.setText("");
MessageRequest request = new MessageRequest.Builder()
.inputText(inputText)
.build();
myConversationService
.message(getString(R.string.workspace), request)
.enqueue(new ServiceCallback<MessageResponse>() {
@Override
public void onResponse(MessageResponse response) {
final String outputText = response.getText().get(0);
runOnUiThread(new Runnable() {
@Override
public void run() {
conversation.append(
Html.fromHtml("<p><b>Bot:</b> " +outputText + "</p>")
);
}
});
}
@Override
public void onFailure(Exception e) {}
});
}
});}}
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ScrollView
android:background="#f5d9d9"
android:layout_width="match_parent"
android:layout_height="300sp"
android:layout_above="@+id/user_input_container">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/conversation"
android:textSize="16sp"
/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:id="@+id/user_input_container">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message"
android:layout_weight="5"
android:id="@+id/user_input"
android:inputType="textShortMessage"/>
<Button
android:id="@+id/sendbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="send"/>
</LinearLayout>
</LinearLayout>
<string name="workspace">a6d8c44e-4ee5-4712-9d7d-d253ba03c2eb</string>
在 IBM Watson 中试用虚拟设备,它运行完美。
但在我的 Android 应用程序中,它没有显示。我想,我的 Android 代码有问题,请帮忙。
[编辑后的代码图像]
基于 Watson Assistant 的示例响应 API
{
"intents": [
{
"intent": "hello",
"confidence": 0.9755029201507568
}
],
"entities": [],
"input": {
"text": "Hello"
},
"output": {
"generic": [
{
"response_type": "text",
"text": "Hello! What can I do for you?"
}
],
"text": [
"Hello! What can I do for you?"
],
"nodes_visited": [
"greeting"
],
"log_messages": []
},
"context": {
"conversation_id": "a96ec62f-773c-4e84-8be9-f9dbca9f83d0",
"system": {
"dialog_stack": [
{
"dialog_node": "root"
}
],
"dialog_turn_counter": 1,
"dialog_request_counter": 1,
"_node_output_map": {
"greeting": {
"0": [
0,
0
]
}
},
"branch_exited": true,
"branch_exited_reason": "completed"
}
}
}
我认为问题在于这行 Android 代码
response.getText().get(0);
收到响应后,您正在尝试读取 getText.get(0) 下的第一个元素。由于它可以是数组或数组对象,因此您应该遍历它以读取每个值。
替换
final String outputText = response.getText().get(0);
与
String outputText = "";
int length=response.getText().size();
Log.i(TAG, "run: "+length);
if(length>1) {
for (int i = 0; i < length; i++) {
outputText += '\n' + response.getText().get(i).trim();
}
}
else
outputText = response.getText().get(0);
1这里是完整的来源 code.I 已尝试但无法在 logcat 中显示 json 响应。我如何才能从 ibm watson 获得完整的响应以及如何在我的聊天机器人中显示它。
public class MainActivity extends AppCompatActivity {
TextView conversation;
EditText userInput;
Button btnSend;
JSONObject jsonObject;
Object object;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conversation = (TextView)findViewById(R.id.conversation);
userInput = (EditText)findViewById(R.id.user_input);
btnSend=(Button) findViewById(R.id.sendbutton);
final ConversationService myConversationService =
new ConversationService(
"2017-05-26",
getString(R.string.username),
getString(R.string.password)
);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "working", Toast.LENGTH_SHORT).show();
final String inputText = userInput.getText().toString();
conversation.append(
Html.fromHtml("<p><b>You:</b> " + inputText + "</p>")
);
userInput.setText("");
MessageRequest request = new MessageRequest.Builder()
.inputText(inputText)
.build();
myConversationService
.message(getString(R.string.workspace), request)
.enqueue(new ServiceCallback<MessageResponse>() {
@Override
public void onResponse(MessageResponse response) {
final String outputText = response.getText().get(0);
runOnUiThread(new Runnable() {
@Override
public void run() {
conversation.append(
Html.fromHtml("<p><b>Bot:</b> " +outputText + "</p>")
);
}
});
}
@Override
public void onFailure(Exception e) {}
});
}
});}}
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ScrollView
android:background="#f5d9d9"
android:layout_width="match_parent"
android:layout_height="300sp"
android:layout_above="@+id/user_input_container">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/conversation"
android:textSize="16sp"
/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:id="@+id/user_input_container">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message"
android:layout_weight="5"
android:id="@+id/user_input"
android:inputType="textShortMessage"/>
<Button
android:id="@+id/sendbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="send"/>
</LinearLayout>
</LinearLayout>
<string name="workspace">a6d8c44e-4ee5-4712-9d7d-d253ba03c2eb</string>
在 IBM Watson 中试用虚拟设备,它运行完美。
但在我的 Android 应用程序中,它没有显示。我想,我的 Android 代码有问题,请帮忙。 [编辑后的代码图像]
基于 Watson Assistant 的示例响应 API
{
"intents": [
{
"intent": "hello",
"confidence": 0.9755029201507568
}
],
"entities": [],
"input": {
"text": "Hello"
},
"output": {
"generic": [
{
"response_type": "text",
"text": "Hello! What can I do for you?"
}
],
"text": [
"Hello! What can I do for you?"
],
"nodes_visited": [
"greeting"
],
"log_messages": []
},
"context": {
"conversation_id": "a96ec62f-773c-4e84-8be9-f9dbca9f83d0",
"system": {
"dialog_stack": [
{
"dialog_node": "root"
}
],
"dialog_turn_counter": 1,
"dialog_request_counter": 1,
"_node_output_map": {
"greeting": {
"0": [
0,
0
]
}
},
"branch_exited": true,
"branch_exited_reason": "completed"
}
}
}
我认为问题在于这行 Android 代码
response.getText().get(0);
收到响应后,您正在尝试读取 getText.get(0) 下的第一个元素。由于它可以是数组或数组对象,因此您应该遍历它以读取每个值。
替换
final String outputText = response.getText().get(0);
与
String outputText = "";
int length=response.getText().size();
Log.i(TAG, "run: "+length);
if(length>1) {
for (int i = 0; i < length; i++) {
outputText += '\n' + response.getText().get(i).trim();
}
}
else
outputText = response.getText().get(0);