Java based Idiom Dictionary interface calling code example

From , 5 Years ago, written in Java, viewed 111 times.
URL https://pastebin.vip/view/6c1da886
  1. import java.io.BufferedReader;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.UnsupportedEncodingException;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.net.URLEncoder;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12.  
  13. import net.sf.json.JSONObject;
  14.  
  15. /**
  16. *成语词典调用示例代码 - 聚合数据
  17. *在线接口文档:http://www.juhe.cn/docs/157
  18. **/
  19.  
  20. public class JuheDemo {
  21.     public static final String DEF_CHATSET = "UTF-8";
  22.     public static final int DEF_CONN_TIMEOUT = 30000;
  23.     public static final int DEF_READ_TIMEOUT = 30000;
  24.     public static String userAgent =  "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
  25.  
  26.     //配置您申请的KEY
  27.     public static final String APPKEY ="*************************";
  28.  
  29.     //1.根据成语查询详细信息
  30.     public static void getRequest1(){
  31.         String result =null;
  32.         String url ="http://v.juhe.cn/chengyu/query";//请求接口地址
  33.         Map params = new HashMap();//请求参数
  34.             params.put("word","");//填写需要查询的汉字,UTF8 urlencode编码
  35.             params.put("key",APPKEY);//应用APPKEY(应用详细页查询)
  36.             params.put("dtype","");//返回数据的格式,xml或json,默认json
  37.  
  38.         try {
  39.             result =net(url, params, "GET");
  40.             JSONObject object = JSONObject.fromObject(result);
  41.             if(object.getInt("error_code")==0){
  42.                 System.out.println(object.get("result"));
  43.             }else{
  44.                 System.out.println(object.get("error_code")+":"+object.get("reason"));
  45.             }
  46.         } catch (Exception e) {
  47.             e.printStackTrace();
  48.         }
  49.     }
  50.  
  51.  
  52.  
  53.     public static void main(String[] args) {
  54.  
  55.     }
  56.  
  57.     /**
  58.      *
  59.      * @param strUrl 请求地址
  60.      * @param params 请求参数
  61.      * @param method 请求方法
  62.      * @return  网络请求字符串
  63.      * @throws Exception
  64.      */
  65.     public static String net(String strUrl, Map params,String method) throws Exception {
  66.         HttpURLConnection conn = null;
  67.         BufferedReader reader = null;
  68.         String rs = null;
  69.         try {
  70.             StringBuffer sb = new StringBuffer();
  71.             if(method==null || method.equals("GET")){
  72.                 strUrl = strUrl+"?"+urlencode(params);
  73.             }
  74.             URL url = new URL(strUrl);
  75.             conn = (HttpURLConnection) url.openConnection();
  76.             if(method==null || method.equals("GET")){
  77.                 conn.setRequestMethod("GET");
  78.             }else{
  79.                 conn.setRequestMethod("POST");
  80.                 conn.setDoOutput(true);
  81.             }
  82.             conn.setRequestProperty("User-agent", userAgent);
  83.             conn.setUseCaches(false);
  84.             conn.setConnectTimeout(DEF_CONN_TIMEOUT);
  85.             conn.setReadTimeout(DEF_READ_TIMEOUT);
  86.             conn.setInstanceFollowRedirects(false);
  87.             conn.connect();
  88.             if (params!= null && method.equals("POST")) {
  89.                 try {
  90.                     DataOutputStream out = new DataOutputStream(conn.getOutputStream());
  91.                         out.writeBytes(urlencode(params));
  92.                 } catch (Exception e) {
  93.                     // TODO: handle exception
  94.                 }
  95.             }
  96.             InputStream is = conn.getInputStream();
  97.             reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
  98.             String strRead = null;
  99.             while ((strRead = reader.readLine()) != null) {
  100.                 sb.append(strRead);
  101.             }
  102.             rs = sb.toString();
  103.         } catch (IOException e) {
  104.             e.printStackTrace();
  105.         } finally {
  106.             if (reader != null) {
  107.                 reader.close();
  108.             }
  109.             if (conn != null) {
  110.                 conn.disconnect();
  111.             }
  112.         }
  113.         return rs;
  114.     }
  115.  
  116.     //将map型转为请求参数型
  117.     public static String urlencode(Map<String,Object>data) {
  118.         StringBuilder sb = new StringBuilder();
  119.         for (Map.Entry i : data.entrySet()) {
  120.             try {
  121.                 sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
  122.             } catch (UnsupportedEncodingException e) {
  123.                 e.printStackTrace();
  124.             }
  125.         }
  126.         return sb.toString();
  127.     }
  128. }

Reply to "Java based Idiom Dictionary interface calling code example"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website