1here is the full souce code.I tried but couldn't able to show the json response in logcat. how can I get the full response from ibm watson & how can I show it in my chatbot.
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>
In IBM Watson try out virtual device, it is working perfectly.
But in my Android app, it's not displaying. I think, there is something wrong in my Android code, please help. [edited code image]
Based on the example response from 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"
}
}
}
I think the issue is with this line of Android code
response.getText().get(0);
Once the response is received you are trying to read the first element under getText.get(0). As it can be an array or an object of arrays, you should loop through it to read every value.
Replace
final String outputText = response.getText().get(0);
with
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);