IBM Watson Assistant:对话框可以在“试用”中工作,但不能在Android应用程序中工作

时间:2018-11-16 07:20:58

标签: android android-studio ibm-cloud watson-conversation

1这是完整的源代码。我试过但无法在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中试用虚拟设备,它运行良好。

In IBM Watson Assistant try out virtual device, it is working perfectly

但是在我的Android应用中,它没有显示。我认为我的Android代码有问题,请帮忙。 [已编辑的代码图像]

1 个答案:

答案 0 :(得分:0)

基于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);