zoukankan      html  css  js  c++  java
  • 百度鹰眼Java接口调用增删改查实例

    因感觉百度鹰眼的使用场景比较符合实际业务,于是对百度鹰眼做了简单功能调试。
    刚开始使用springframework封装的RestTemplate,但是测试提示ak参数不存在。
    后又试了几种方法,均提示ak参数不存在。但是ak参数明明存在的,且是正确的(可能本人参数设置问题)。
    百度相关ak参数不存在问题,发现还有一部分人遇到这个问题。
    经过参考对应资料,终于把基础的增删改查业务调试通过。特做简单整理,既能自我总结又能分享给万一有需要的朋友。

      1 import java.io.IOException;
      2 import java.io.InputStream;
      3 import java.io.OutputStreamWriter;
      4 import java.net.URL;
      5 import java.net.URLConnection;
      6 import org.apache.commons.io.IOUtils;
      7 
      8 /**
      9  * ClassName: YingYanUtil 
     10  * @Description: 鹰眼请求工具类
     11  * @author ljwang
     12  * @date 2017年9月4日
     13  */
     14 public class YingYanUtil {
     15     
     16     /**
     17      * 百度鹰眼服务器端Key
     18      */
     19     public static final String BAIDU_YINGYAN_AK_S = "*********************";
     20     
     21     /**
     22      * 百度鹰眼服务ID
     23      */
     24     public static final String BAIDU_YINGYAN_SERVICE_ID = "***************";    
     25     
     26     /**
     27      * 百度鹰眼接口URL
     28      */
     29     public static final String BAIDU_YINGYAN_URL = "http://yingyan.baidu.com/api/v3/";
     30     
     31     /**
     32      * 百度鹰眼请求方式(POST)
     33      */
     34     public static final String BAIDU_YINGYAN_REQ_POST = "POST";
     35     
     36     /**
     37      * 百度鹰眼请求方式(GET)
     38      */
     39     public static final String BAIDU_YINGYAN_REQ_GET = "GET";    
     40     
     41     
     42     /**
     43      * ClassName: BaiduYingyanEntity 
     44      * @Description: 终端管理
     45      * @author ljwang
     46      * @date 2017年9月4日
     47      */
     48     public static class BaiduYingyanEntity {
     49         /**
     50          * 创建entity,并赋属性信息
     51          */
     52         public static final String ADD = "entity/add";
     53         
     54         /**
     55          * 删除entity
     56          */
     57         public static final String DELETE = "entity/delete";
     58         
     59         /**
     60          * 更新entity属性信息
     61          */
     62         public static final String UPDATE = "entity/update";
     63         
     64         /**
     65          * 检索符合条件的entity,返回entity属性信息和最新位置。可用于列出entity,也可用于批量查询多个entitiy的位置
     66          */
     67         public static final String LIST = "entity/list";
     68     }
     69     
     70 
     71     /**
     72      * @Description: 请求鹰眼
     73      * @author ljwang
     74      * @date 2017年9月4日
     75      */
     76     public static String yingYanReq(String urlReq, String param, String method) {
     77         try {
     78             //Get请求,拼装参数
     79             if (BAIDU_YINGYAN_REQ_GET.equals(method)) {
     80                 urlReq = urlReq + "?" + param;
     81             }
     82             
     83             //创建URL对象
     84             URL url = new URL(urlReq);
     85             //返回一个URLConnection对象,它表示到URL所引用的远程对象的连接
     86             URLConnection urlConnection = url.openConnection();
     87             
     88             //POST请求,写入参数
     89             if (BAIDU_YINGYAN_REQ_POST.equals(method)) {
     90                 // 设置doOutput属性为true表示将使用此urlConnection写入数据
     91                 urlConnection.setDoOutput(true);
     92                 // 定义待写入数据的内容类型,我们设置为application/x-www-form-urlencoded类型
     93                 urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
     94                 // 得到请求的输出流对象
     95                 OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
     96                 // 把数据写入请求的Body
     97                 out.write(param);
     98                 out.flush();
     99                 out.close();
    100             }
    101             
    102             // 从服务器读取响应
    103             InputStream inputStream = urlConnection.getInputStream();
    104             String encoding = urlConnection.getContentEncoding();
    105             String result = IOUtils.toString(inputStream, encoding);
    106             System.out.println(result);
    107             return result;
    108         } catch (IOException e) {
    109             e.printStackTrace();
    110             return null;
    111         }
    112     }    
    113     
    114     public static void main(String[] args){
    115         
    116         /*
    117          * 添加entity
    118          */
    119         String urlReq = BAIDU_YINGYAN_URL + BaiduYingyanEntity.ADD;
    120         String param = "ak=" + BAIDU_YINGYAN_AK_S +
    121                        "&service_id=" + BAIDU_YINGYAN_SERVICE_ID +
    122                        "&entity_name=" + "鹰眼测试名称" +
    123                        "&entity_desc=" + "鹰眼测试描述";
    124         yingYanReq(urlReq, param, BAIDU_YINGYAN_REQ_POST);
    125         
    126         /*
    127          * 查询entity
    128          */
    129 //        String urlReq = BAIDU_YINGYAN_URL + BaiduYingyanEntity.LIST;
    130 //        String param = "ak=" + BAIDU_YINGYAN_AK_S +
    131 //                       "&service_id=" + BAIDU_YINGYAN_SERVICE_ID + 
    132 //                       "&filter=entity_names:鹰眼测试名称";
    133 //        yingYanReq(urlReq, param, BAIDU_YINGYAN_REQ_GET);
    134         
    135         /*
    136          * 修改entity
    137          */
    138 //        String urlReq = BAIDU_YINGYAN_URL + BaiduYingyanEntity.UPDATE;
    139 //        String param = "ak=" + BAIDU_YINGYAN_AK_S +
    140 //                       "&service_id=" + BAIDU_YINGYAN_SERVICE_ID +
    141 //                       "&entity_name=" + "鹰眼测试名称" +
    142 //                       "&entity_desc=" + "鹰眼修改测试";
    143 //        yingYanReq(urlReq, param, BAIDU_YINGYAN_REQ_POST);
    144         
    145         /*
    146          * 删除entity
    147          */
    148 //        String urlReq = BAIDU_YINGYAN_URL + BaiduYingyanEntity.DELETE;
    149 //        String param = "ak=" + BAIDU_YINGYAN_AK_S +
    150 //                       "&service_id=" + BAIDU_YINGYAN_SERVICE_ID +
    151 //                       "&entity_name=" + "鹰眼测试名称";
    152 //        yingYanReq(urlReq, param, BAIDU_YINGYAN_REQ_POST);
    153     }
    154 }

    作者:行舟逐梦

    出处:http://www.cnblogs.com/liujianwang

    感谢您的认真阅读。不足之处,欢迎指正。

  • 相关阅读:
    「数列分块入门学习笔记」
    「Luogu P3273 [SCOI2011]棘手的操作」
    「CF1342D Multiple Testcases」
    「CF803G Periodic RMQ Problem」
    【cf比赛记录】Educational Codeforces Round 77 (Rated for Div. 2)
    【cf比赛记录】Codeforces Round #601 (Div. 2)
    【cf比赛记录】Codeforces Round #600 (Div. 2)
    【学习报告】简单搜索
    【POJ2676】Sudoku
    【POJ1416】Shredding Company
  • 原文地址:https://www.cnblogs.com/liujianwang/p/7473684.html
Copyright © 2011-2022 走看看