图灵机器人学习

一与机器人聊天

APKEY: 图灵官网申请

MSG :聊天内容

package com.tuling.robot;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class TuLingIntelligentRobot {
	public static void main(String[] args) throws   Exception {
		String APKEY ="b9c73434132c08a17a70eb977d743f3b";
		String MSG ="北京天气";
		String INFOR=URLEncoder.encode(MSG, "utf-8");
		String addrURL="http://www.tuling123.com/openapi/api?key="+APKEY+"&info="+INFOR ;
		URL getUrl = new URL(addrURL);
		HttpURLConnection connection =(HttpURLConnection)getUrl.openConnection();
		connection.connect();
		 // 取得输入流,并使用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();
		connection.disconnect();
		System.out.println(sb);
	}

}
内容:

{"code":100000,"text":"北京:05/05 周二,12-27° 27° 多云转晴 北风3-4 级;立夏 周三,10-23° 阴 南风3-4 级;05/07 周四,14-25° 晴 无风向微风;05/08 周五,12-24° 阴 无风向微风;"}

图灵机器人学习二

public static void getMsg02() throws Exception {
		String APKEY = "b9c73434132c08a17a70eb977d743f3b";
		String INFOR = URLEncoder.encode("北京到郑州的火车", "utf-8");
		String requesturl = "http://www.tuling123.com/openapi/api?key=" + APKEY+ "&info=" + INFOR;
		HttpGet request = new HttpGet(requesturl);
		HttpResponse response = HttpClients.createDefault().execute(request);
		// 200即正确的返回码
		if (response.getStatusLine().getStatusCode() == 200) {
			String result = EntityUtils.toString(response.getEntity());
			System.out.println("返回结果:" + result);
		}
	}


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐