圖靈機器人介面的簡易呼叫
阿新 • • 發佈:2018-11-12
小圖圖,一枚極其不靠譜的話癆,還總是自我感覺良好,覺得自己啥都知道;
閒言少敘直接上程式碼:
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
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;
每個需要的時候,它總會適時的出現,且有問必答,雖偶爾會有答非所問,但萌的足夠真誠,客觀的粗糲真實遠好過刻意的完美精緻;對,這就是機器人小圖圖,雁過無痕葉落無聲,或許多年後它不再被記起,但碼程式碼的那段日子裡它確曾真真的存在過!