中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

基于JAVA的違章查詢助手?jǐn)?shù)據(jù)調(diào)用代碼實(shí)例

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

[Java]代碼    

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
 
import net.sf.json.JSONObject;
 
/**
*全國(guó)車輛違章調(diào)用示例代碼 - 聚合數(shù)據(jù)
*在線接口文檔:http://www.juhe.cn/docs/36
**/
 
public class JuheDemo {
    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";
 
    //配置您申請(qǐng)的KEY
    public static final String APPKEY ="*************************";
 
    //1.獲取支持城市參數(shù)接口
    public static void getRequest1(){
        String result =null;
        String url ="http://v.juhe.cn/wz/citys";//請(qǐng)求接口地址
        Map params = new HashMap();//請(qǐng)求參數(shù)
            params.put("province","");//默認(rèn)全部,省份簡(jiǎn)寫(xiě),如:ZJ、JS
            params.put("dtype","");//返回?cái)?shù)據(jù)格式:json或xml或jsonp,默認(rèn)json
            params.put("format","");//格式選擇1或2,默認(rèn)1
            params.put("callback","");//返回格式選擇jsonp時(shí),必須傳遞
            params.put("key",APPKEY);//你申請(qǐng)的key
 
        try {
            result =net(url, params, "GET");
            JSONObject object = JSONObject.fromObject(result);
            if(object.getInt("error_code")==0){
                System.out.println(object.get("result"));
            }else{
                System.out.println(object.get("error_code")+":"+object.get("reason"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //2.請(qǐng)求違章查詢接口
    public static void getRequest2(){
        String result =null;
        String url ="http://v.juhe.cn/wz/query";//請(qǐng)求接口地址
        Map params = new HashMap();//請(qǐng)求參數(shù)
            params.put("dtype","");//返回?cái)?shù)據(jù)格式:json或xml或jsonp,默認(rèn)json
            params.put("callback","");//返回格式選擇jsonp時(shí),必須傳遞
            params.put("key",APPKEY);//你申請(qǐng)的key
            params.put("city","");//城市代碼 *
            params.put("hphm","");//號(hào)牌號(hào)碼 完整7位 ,需要utf8 urlencode*
            params.put("hpzl","");//號(hào)牌類型,默認(rèn)02
            params.put("engineno","");//發(fā)動(dòng)機(jī)號(hào) (根據(jù)城市接口中的參數(shù)填寫(xiě))
            params.put("classno","");//車架號(hào) (根據(jù)城市接口中的參數(shù)填寫(xiě))
 
        try {
            result =net(url, params, "GET");
            JSONObject object = JSONObject.fromObject(result);
            if(object.getInt("error_code")==0){
                System.out.println(object.get("result"));
            }else{
                System.out.println(object.get("error_code")+":"+object.get("reason"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //3.接口剩余請(qǐng)求次數(shù)查詢
    public static void getRequest3(){
        String result =null;
        String url ="http://v.juhe.cn/wz/status";//請(qǐng)求接口地址
        Map params = new HashMap();//請(qǐng)求參數(shù)
            params.put("key",APPKEY);//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁(yè)查詢)
            params.put("dtype","");//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)json
 
        try {
            result =net(url, params, "GET");
            JSONObject object = JSONObject.fromObject(result);
            if(object.getInt("error_code")==0){
                System.out.println(object.get("result"));
            }else{
                System.out.println(object.get("error_code")+":"+object.get("reason"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
 
    public static void main(String[] args) {
 
    }
 
    /**
     *
     * @param strUrl 請(qǐng)求地址
     * @param params 請(qǐng)求參數(shù)
     * @param method 請(qǐng)求方法
     * @return  網(wǎng)絡(luò)請(qǐng)求字符串
     * @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型轉(zhuǎn)為請(qǐng)求參數(shù)型
    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();
    }
}

標(biāo)簽: 代碼 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:金額大寫(xiě)轉(zhuǎn)小寫(xiě)SQL

下一篇:MD5加密Java工具類