Java代码调用聚合数据---查询全国车辆违章接口返回违章结果

1、注册聚合数据账号,完成认证,申请数据接口

打开https://www.juhe.cn/,点击最右侧的注册

输入自己的信息注册就行了

注册完成后,认证一下,公司认证或者个人认证都可以,但是调用全国车辆违章的查询要求必须是企业认证

认证完成后进入个人中心点击左侧的我的接口,点击申请新数据

选择交通地理,全国车辆违章,最后点击立即申请

申请完成后,会给你生成一个AppKey

这个AppKey是调用聚合数据接口时必须用到的

拿到这个AppKey后,现在看下聚合数据官网提供的文档

打开https://www.juhe.cn/,点击API

到这个页面,官方提供了java调用的代码示例,你只需要把示例里的AppKey换成你自己申请的AppKey就可以了

https://code.juhe.cn/docs/775

2、写代码实现调用

有了上述的准备后,就可以写代码来调用一下了

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 调用聚合数据,查看违章
 */
@RestController
@Slf4j
public class ViolationController {

    public static final String DEF_CHATSET = "UTF-8";
    public static final int DEF_CONN_TIMEOUT = 30000;
    public static final int DEF_READ_TIMEOUT = 30000;
    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";

    @RequestMapping(value = "/getViolationList", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String getViolationList(){
        Map<String, Object> returnMap = new HashMap<>(16);
        String result =null;
        String url ="http://v.juhe.cn/wz/query";//请求接口地址
        Map params = new HashMap();//请求参数
        //params.put("dtype","");//返回数据格式:json或xml或jsonp,默认json
        //params.put("callback","");//返回格式选择jsonp时,必须传递
        //params.put("city","");//城市代码 *
        //params.put("hpzl","");//号牌类型,默认02
        params.put("key","xxxxxxxxxxxxxxxxxxxxxx");//你申请的key
        params.put("hphm","京Q88888");//号牌号码 完整7位 ,需要utf8 urlencode*
        params.put("engineno","K3U88888888");//发动机号 (根据城市接口中的参数填写)
        params.put("classno","xxxxxxxxxxxxxxxx");//车架号 VIN码(根据城市接口中的参数填写)
        try {
            result =net(url, params, "GET");
            JSONObject json = JSONObject.parseObject(result);
            if (json.getInteger("resultcode") == 200) {
                JSONObject result1 = json.getJSONObject("result");
                JSONArray lists = result1.getJSONArray("lists");
                returnMap.put("lists", lists);
                returnMap.put("respCode", 0);
                returnMap.put("respMsg", "成功");
            } else {
                returnMap.put("error_code", json.get("error_code"));
                returnMap.put("reason", json.get("reason"));
                returnMap.put("respCode", -1);
                returnMap.put("respMsg", "失败");
            }
            return JSONObject.toJSONString(returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *
     * @param strUrl 请求地址
     * @param params 请求参数
     * @param method 请求方法
     * @return  网络请求字符串
     * @throws Exception
     */
    public static String net(String strUrl, Map params,String method) throws Exception {
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String rs = null;
        try {
            StringBuffer sb = new StringBuffer();
            if(method==null || method.equals("GET")){
                strUrl = strUrl+"?"+urlencode(params);
            }
            URL url = new URL(strUrl);
            conn = (HttpURLConnection) url.openConnection();
            if(method==null || method.equals("GET")){
                conn.setRequestMethod("GET");
            }else{
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
            }
            conn.setRequestProperty("User-agent", userAgent);
            conn.setUseCaches(false);
            conn.setConnectTimeout(DEF_CONN_TIMEOUT);
            conn.setReadTimeout(DEF_READ_TIMEOUT);
            conn.setInstanceFollowRedirects(false);
            conn.connect();
            if (params!= null && method.equals("POST")) {
                try {
                    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
                    out.writeBytes(urlencode(params));
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            InputStream is = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sb.append(strRead);
            }
            rs = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return rs;
    }

    //将map型转为请求参数型
    public static String urlencode(Map<String,Object>data) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry i : data.entrySet()) {
            try {
                sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}


写完后用postMan调用一下试试

可以看到已经返回违章的结果记录了

发布了108 篇原创文章 · 获赞 103 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/ju_362204801/article/details/105285185
今日推荐