Search code examples
javaandroidjsonibm-cloudwatson-conversation

Unknown pattern character 'X' Exception occurs connecting to IBM Watson conversation API from Android


I am working with some chat bot application on Android. Everything was working perfect before. But the same application that worked before, now shows an error. "Unknown pattern character 'X' " . Don't know what i did wrong. I tried a lot to find the bug but failed. I am attaching my code here..

package com.example.rove2.conversation;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.ibm.watson.developer_cloud.conversation.v1.ConversationService;
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest;
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
String conversation_username;
String  conversation_password;
String workspace_id;
Button sendbtn,clearbtn,jsonbtn;
EditText input,json;
String JsonString="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    conversation_username = "c9d2fd27-0868-4e56-b3ae-66b76d2b52b4";
    conversation_password = "DYLntDWRvf12";
    workspace_id="cbb7fc4d-c216-4a81-ae70-445d6fa9dab9";
    sendbtn=(Button)findViewById(R.id.button);
    clearbtn=(Button)findViewById(R.id.button2);
    input = (EditText)findViewById(R.id.editText);
    json =(EditText)findViewById(R.id.editText2);
    jsonbtn=(Button)findViewById(R.id.button3);
}
String sendtoWatson(String message)
{
    ConversationService service = new 
ConversationService(ConversationService.VERSION_DATE_2017_02_03);
    service.setUsernameAndPassword(conversation_username, 
conversation_password);
    MessageRequest newMessage = new 
MessageRequest.Builder().inputText(message).build();
    MessageResponse response = service.message(workspace_id, 
newMessage).execute();
    String tx = response.toString();
    return  tx;
}
public void send(View view){
    Thread t=new Thread(){
        public void run() {
            String text = input.getText().toString();
            final String result = sendtoWatson(text);
            JsonString = result;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try{
                        JSONObject reader = new JSONObject(result);
                        JSONObject output = reader.getJSONObject("output");
                        JSONArray Intent = output.getJSONArray("text");
                        String speechTxt=Intent.getString(0);
                        json.setText(speechTxt);

                    }
                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }};
    t.start();
}
public void clearbutn(View view){
    input.setText("");
    json.setText("");
}
public void jsonbtn(View view){
    json.setText(JsonString);
}
}

The error occurs at the line

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

I could find an answer for a similar issue here BlueMix service error with Android app - 'Unknown pattern character 'X'' But i am not able to understand the solution my android studio version is 2.1.2 and JDK version is 8. Please help...


Solution

  • This is a known issue that was fixed But not released to maven yet. If you look at the README in the repository, there are instructions on how to use the snapshot release while we wait for 3.8.1.