其他分享
首页 > 其他分享> > 2021-09-07

2021-09-07

作者:互联网

JAVA实现成语大全

提示:公司开发的APP涉及到成语教育,所以需要用到成语相关的数据:成语大全API 正好有提供成语大全API接口,直接项目中调用就搞定了。


问题描述:

API接口返回的数据格式如下:

{
    "code":"AJAX_SUCCESS",
    "msg":"操作成功",
    "data":{
        "word":"凡事预则立,不预则废",
        "pinyinFull":"fán shì yù zé lì,bù yù zé fèi",
        "len":10,
        "pinyinFirst":"fsyzlbyzf",
        "means":" 预:预先,指事先作好计划或准备;立:成就;废:败坏。不论做什么事,事先有准备,就能得到成功,不然就会失败。",
        "from":" 《礼记·中庸》:“凡事预则立,不预则废。”",
        "oldFont":" 凡事預則立,不預則廢",
        "useFrequency":" 常用成语",
        "color":" 中性成语",
        "useMethod":" 复句式;作分句;指有准备就会成功",
        "frame":" 复句式成语",
        "sameMean":" 凡事豫则立,不豫则废",
        "backMean":" 执迷不悟 、 死不改悔",
        "example":" “凡事预则立,不预则废”,没有事先的计划和准备,就不能获得战争的胜利。(《毛泽东选集·论持久战》)"
    },
    "timeMs":8,
    "success":true
}

JAVA代码如下:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 成语大全 示例代码
 **/
public class PostTest {

	public static void main(String[] args) throws Exception {
		String domain = "http://api.data5u.com";
		String api = "/dict/chengyu";
		String params = "appsec=&word=";   // 参数值请自行补全
		String method = "POST";
		
    	HttpURLConnection connection = null;
    	URL link = new URL(domain + api);
    
		connection = (HttpURLConnection)link.openConnection();
		
		connection.setRequestMethod(method); 
    	connection.setDoOutput(true);
    	connection.setUseCaches(false);
    	connection.setReadTimeout(10000);
    	
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(),"UTF-8"));
        out.write(params);
        out.flush();
        out.close();   
    	
        String line = null;
        StringBuilder html = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        while((line = reader.readLine()) != null){
        	html.append(line);
        }
        
        try {
			if (reader != null) {
				reader.close();
			}
		} catch (Exception e) {
		}
        
        System.out.println(html.toString());
	}

}

标签:java,07,09,String,connection,2021,import,new,成语
来源: https://blog.csdn.net/u010978757/article/details/120155710