我的問題是我想從webservice讀取數據並將其打印在 chatbot中。如何從webservice讀取數據並使用java代碼在chatbot中打印
web服務將有事件數量及其在它狀態,我必須將數據從互聯網服務和打印複印。我嘗試使用下面的代碼,但我不確定如何在對話聊天機器人中打印這些數據。
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws IOException {
String requestMessage = request.getParameter("message");
String contextString = request.getParameter("context");
JSONObject contextObject = new JSONObject();
if(contextString != null) {enter code here
contextObject = JSONObject.parseObject(contextString);
}
System.out.println("Context: ");
System.out.println(contextObject);
Map<String, Object> contextMap = Utility.toMap(contextObject);
if(requestMessage == null || requestMessage.isEmpty()){
requestMessage = "Greetings";
}
if(requestMessage == "1" || requestMessage == "merge id"){
// throw new NullPointerException();
requestMessage = "call";
try {
URL e = new URL(
"webservice url");
HttpURLConnection conn = (HttpURLConnection) e.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "TEXT/PLAIN");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " +
conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
System.out.println("Output from Server .... \n");
String output;
您可以將數據作爲上下文變量傳遞給對話。然後可以在對話節點中使用上下文變量,例如在響應中打印。 –
感謝您的回覆,請以示例幫助我,我是watson和java的新手。 – user2319726
我假設你已經閱讀過這個:https://www.ibm.com/watson/developercloud/doc/conversation/dialog-context.html –