1.青云客
官网:http://api.qingyunke.com/
2.添加依赖
<!--okhttp3 依赖-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
3.主程序
package org.example;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import java.io.IOException;
public class reply {
public static void main(String[] args) throws IOException {
ok();
}
public static void ok(){
new Thread(new Runnable() {
@Override
public void run() {
try {
String data = "今天天气不错!";
String url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg="+data;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
okhttp3.Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
assert response.body() != null;
System.out.println(response);
String re = response.body().string();
// 使用 JsonParser 解析字符串为 JsonObject
JsonObject jsonObject = JsonParser.parseString(re).getAsJsonObject();
// 获取 content 字段的值
String content = jsonObject.get("content").getAsString();
// 打印提取的内容
System.out.println("回答:"+content);
} else {
System.out.println("error");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}