【问题标题】:IBM Watson Assistant: Dialog working in "Try it out" but not in Android appIBM Watson Assistant:对话框在“试用”中工作,但不在 Android 应用程序中
【发布时间】:2018-11-16 07:20:58
【问题描述】:

1here 是完整的源代码。我试过但无法在 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 代码有问题,请帮忙。 [编辑代码图像]

【问题讨论】:

  • 在对话框中包含详细信息。 “试用”和应用中的 API 版本是否相同? “试试看”中显示的错误是什么?
  • 我不确定您是如何看到两个响应的。你能提供 JSON 响应吗?
  • 是的。 API版本相同。
  • 如何找到 JSON 响应??我是应用程序开发的新手。请帮帮我
  • 使用输出文本上方的 Log.i 打印响应 JSON。您可以在 Android Studio 的 Logcat 面板中看到输出。有关日志记录的更多信息,请查看此链接code.tutsplus.com/tutorials/…

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


【解决方案1】:

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

【讨论】:

  • 字符串输出文本 = response.getText().get(0); // 这就是问题所在。对 ??什么是完美的解决方案??
  • 我怎样才能打印剩下的部分??请帮忙。显示完整数据的完美代码是什么?
  • 如上所述,您能否提供我正在寻找的 JSON 响应。因为这将帮助我查看输出是什么,并基于此我可以在这里提出代码
  • 我在提供工作区 ID 和完整代码的地方编辑了我的问题。我尝试过,但找不到 json 响应。现在你能帮我吗?
  • @jannatsuha 强烈建议不要在公共论坛以及与任何人共享凭据。请立即删除凭据
猜你喜欢
  • 2020-12-07
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 2019-07-27
  • 2019-09-01
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多