A lot of people have played, which has the function of chatting with the robot:
This Turing Man Web site provides the relevant API interface that can be used in the program, as follows:
1. Register your account with the Turing Network (http://www.tuling123.com/openapi/cloud/home.jsp) and then he will give you the corresponding API KEY, which needs to be used in the program:
2. Access the robot in the Java program:
String APIKEY = "Official website to your API key";
String INFO = Urlencoder.encode ("Hello, Beauty", "utf-8");//You can enter a question here
String GetURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + info;
URL getUrl = new URL (getUrl);
HttpURLConnection connection = (httpurlconnection) geturl.openconnection ();
Connection.connect ();
Get the input stream and read it using reader
BufferedReader reader = new BufferedReader (New InputStreamReader (Connection.getinputstream (), "Utf-8"));
StringBuffer sb = new StringBuffer ();
String line = "";
while (line = Reader.readline ()) = null) {
Sb.append (line);
}
Reader.close ();
Disconnect Connection
Connection.disconnect ();
This part of the code is connected to the website of the Turing, the next part is to read his reply message. The question you want to ask is put in the string "info".
3. A simple Chat program:
Importjava.awt.FlowLayout;ImportJava.awt.Font;Importjava.awt.GridLayout;ImportJava.awt.Image;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;Importjava.awt.event.KeyEvent;ImportJava.awt.event.KeyListener;ImportJava.beans.EventHandler;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportJava.net.URLEncoder;ImportJavax.swing.Icon;ImportJavax.swing.ImageIcon;ImportJavax.swing.JButton;ImportJavax.swing.JColorChooser;ImportJavax.swing.JDialog;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel;ImportJavax.swing.JTextArea;ImportJavax.swing.JTextField; Public classTuring device man { PublicTuring Man () {} Public Static voidMain (string[] args)throwsIOException {String information=NewString ("Hello"); String Mechine=NewString (); JFrame Frame=NewJFrame ("Chat with robot"); JPanel Panel=NewJPanel (NewGridLayout (3,1)); JPanel Questionpanel=NewJPanel (NewFlowLayout ()); JPanel Buttonpanel=NewJPanel (); JPanel Answerpanel=NewJPanel (NewFlowLayout ()); JLabel question=NewJLabel ("Problem"); JTextField enterquestion=NewJTextField (20); JLabel Answer=NewJLabel ("Robot Answer"); JTextArea Enteranswer=NewJTextArea (3,25); JButton Submit=NewJButton ("Submit");//ImageIcon Imgicon = new ImageIcon ("Images/robot.png");//Icon img = imgicon;//JLabel Imglabel = new JLabel ();Frame.setsize (600, 400); Frame.setdefaultcloseoperation (Jframe.exit_on_close); Frame.setvisible (true); Enteranswer.setfont (NewFont ("Arial", font.bold,15)); Enterquestion.setfont (NewFont ("Arial", font.bold,15));//Imgicon.setimage (Imgicon.getimage (). getScaledInstance (35,35,image.scale_default));Enteranswer.setlinewrap (true);//Imglabel.seticon (IMG);Questionpanel.add (question); Questionpanel.add (enterquestion);//Answerpanel.add (Imglabel);Answerpanel.add (answer); Answerpanel.add (Enteranswer); Buttonpanel.add (Submit); Panel.add (Questionpanel); Panel.add (Answerpanel); Panel.add (Buttonpanel); Frame.add (panel); Submit.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {String answer=NewString (); String Q=Enterquestion.gettext (); Try{Answer=Machine (q); } Catch(IOException E1) {e1.printstacktrace (); } enteranswer.settext (answer); } }); Enterquestion.addkeylistener (NewKeyListener () {@Override Public voidkeytyped (KeyEvent e) {//TODO auto-generated Method Stub} @Override Public voidkeypressed (KeyEvent e) {if(E.getkeycode () ==10 | | E.getkeycode () ==38) {String answer=NewString (); String Q=Enterquestion.gettext (); Try{Answer=Machine (q); } Catch(IOException E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } enteranswer.settext (answer); }} @Override Public voidkeyreleased (KeyEvent e) {//TODO auto-generated Method Stub } }); } Private StaticString Machine (String quesiton)throwsIOException {//access robot, input problemString APIKEY = "website to your API key"; String INFO= Urlencoder.encode (Quesiton, "utf-8");//You can enter a question hereString GetURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" +INFO; URL GETURL=NewURL (GetURL); HttpURLConnection Connection=(HttpURLConnection) geturl.openconnection (); Connection.connect (); //get the input stream and read it using readerBufferedReader reader =NewBufferedReader (NewInputStreamReader (Connection.getinputstream (), "Utf-8")); StringBuffer SB=NewStringBuffer (); String Line= ""; while(line = Reader.readline ())! =NULL) {sb.append (line); } reader.close (); //Disconnect ConnectionConnection.disconnect (); String[] SS=NewString[10]; String s=sb.tostring (); String answer; SS= S.split (":"); Answer= Ss[ss.length-1]; Answer= Answer.substring (1,answer.length ()-2); returnanswer; }}
:
Summary: This robot is still quite fun, can develop other functions, left to see their own imagination.
Java Access Turing machine person, realize and robot chat