MessageResponse在Watson Conversation中返回空指针

时间:2017-09-07 11:31:38

标签: ibm-cloud ibm-watson chatbot watson-conversation

MessageResponse提供NullPointerException

ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_09_20);
// Credentials of Workspace of Conversation
service.setApiKey("API_KEY");
service.setUsernameAndPassword("USERNAME", "PASSWORD");
MessageRequest newMessage = new MessageRequest.Builder()
  .inputText(request.getQuery())
  .build();

// Workspace ID of Conversation current workspace
String workspaceId = "WORKSPACEID";
service.setSkipAuthentication(true);
MessageResponse response = service.message(workspaceId, newMessage)
  .execute();

2 个答案:

答案 0 :(得分:3)

根据IBM开发人员(@German)的说法:" Watson服务目前使用Basic Auth,因此您将使用用户名和密码代替api_key。要获取凭据,您需要将要使用的服务(例如问题和答案)绑定到Bluemix应用程序。"

请查看以下示例。

尝试使用Java SDK中的以下代码:

ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2017_05_26);
service.setUsernameAndPassword("<username>", "<password>"); //Please make sure if this username and password is the Service Credentials from the Service that you have created to use Conversation

InputData input = new InputData.Builder("Hi").build();
MessageOptions options = new MessageOptions.Builder(workspaceId).input(input).build();

// sync
MessageResponse response = service.message(options).execute();
System.out.println(response);

其他例子:

MessageRequest newMessage = new MessageRequest.Builder().inputText(input).context(context).build();

MessageResponse response = service.message(WORKSPACE_ID,newMessage).execute();

context = response.getContext();    
System.out.println(response);
  • 您可以使用Watson Conversation查看 Official example
  • 请参阅“使用Watson服务的入门指南”(步骤5)here

答案 1 :(得分:2)

对话服务不使用api_keyusernamepassword

您的代码段中有两个错误:  1.使用对话时不需要setApiKey()。  1. service.setSkipAuthentication(true);将指示SDK忽略服务凭据,因此,它们不会在每次请求时发送到服务器。

您只需要删除第service.setSkipAuthentication(true);行。

ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_09_20);
// Credentials of Workspace of Conversation
// BTW: This are not your Bluemix credentials!
service.setUsernameAndPassword("USERNAME", "PASSWORD");

MessageRequest newMessage = new MessageRequest.Builder()
  .inputText("Hi! this is my first message to Watson")
  .build();

MessageResponse response = service.message("WORKSPACEID", newMessage)
  .execute();
System.out.println(response);