调用第三方api之图灵机器人
2016-04-29 20:07
483 查看
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
/**
* 第三方接口
*
* @author Administrator
*
*/
public class ThirdInterface {
public static void question(String info) throws IOException {
URL url = new URL(
"http://www.tuling123.com/openapi/api?key=026efbd4eda9a99726a1d939fab35acc&info="
+ URLEncoder.encode(info, "UTF-8"));
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 5000如果没有连接,抛出异常
httpURLConnection.setConnectTimeout(5000);
// 连续并响应成功
if (httpURLConnection.getResponseCode() == 200) {
try (InputStream inputStream = httpURLConnection.getInputStream();
Scanner scanner = new Scanner(inputStream);) {
scanner.hasNext();
String result = scanner.nextLine();
System.out.println("answer:"
+ result.substring(result.indexOf("\":\"") + 3,
result.indexOf("\"}")));
}
}
}
public static void main(String[] args) throws IOException {
question("");
}
}
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
/**
* 第三方接口
*
* @author Administrator
*
*/
public class ThirdInterface {
public static void question(String info) throws IOException {
URL url = new URL(
"http://www.tuling123.com/openapi/api?key=026efbd4eda9a99726a1d939fab35acc&info="
+ URLEncoder.encode(info, "UTF-8"));
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 5000如果没有连接,抛出异常
httpURLConnection.setConnectTimeout(5000);
// 连续并响应成功
if (httpURLConnection.getResponseCode() == 200) {
try (InputStream inputStream = httpURLConnection.getInputStream();
Scanner scanner = new Scanner(inputStream);) {
scanner.hasNext();
String result = scanner.nextLine();
System.out.println("answer:"
+ result.substring(result.indexOf("\":\"") + 3,
result.indexOf("\"}")));
}
}
}
public static void main(String[] args) throws IOException {
question("");
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Manifest 用法
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 什么是 GraphQL?
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- Spark RDD API详解(一) Map和Reduce
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序