图灵机器人接口的简易调用

小图图,一枚极其不靠谱的话痨,还总是自我感觉良好,觉得自己啥都知道;

闲言少叙直接上代码:

public static void main(String[] args) throws IOException {
        System.out.println("a interesting story begin...");
        while(true){
            System.out.println("Jonathan:");
            Scanner scan = new Scanner(System.in);
            String request= scan.nextLine();
            String info = URLEncoder.encode(request, "utf-8");
            String APIkey = "01baf8196d314e4e962e20a8b561fe67";  //这个是需要自行申请的;
            String getUrl = "http://www.tuling123.com/openapi/api?key="
                            +APIkey+"&info="+info;
            URL url = new URL(getUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.connect();
            
            InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is,"utf-8");
            BufferedReader br = new BufferedReader(isr);
            String line = "";
            StringBuffer sb = new StringBuffer();
            while((line = br.readLine()) != null){
                sb.append(line);
            }
            String sb2 = sb.substring(sb.lastIndexOf(":"));
            System.out.println("小图图"+sb2.toString());
        }
//        is.close();
//        br.close();
//        conn.disconnect();
    }

找个IDE,贴上代码,跑起来出现对话框即ok;

每个需要的时候,它总会适时的出现,且有问必答,虽偶尔会有答非所问,但萌的足够真诚,客观的粗粝真实远好过刻意的完美精致;对,这就是机器人小图图,雁过无痕叶落无声,或许多年后它不再被记起,但码代码的那段日子里它确曾真真的存在过!

猜你喜欢

转载自blog.csdn.net/weixin_39039342/article/details/82942206