package com.sifang.modules.order.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 *
 */
public class TestGaode {
    private static String API = "http://restapi.amap.com/v3/geocode/geo?key=<key>&s=rsv3&address=<address>";

    private static String KEY = "aa4a48297242d22d2b3fd6eddfe62217";

    private static final String DEFAULT_CHARSET = "UTF-8";

    private static Pattern pattern = Pattern.compile("\"location\":\"(\\d+\\.\\d+),(\\d+\\.\\d+)\"");

    static {
        init();
    }

    private static void init() {
        System.out.println("高德地图工具类初始化");
        System.out.println("api: {}"+API);
        System.out.println("key: {}"+KEY);
        API = API.replaceAll("<key>", KEY);
    }


    /**
     * 接口调用 GET
     */
    public static double[] httpURLConectionGET(String address) {
        try {
            String requestUrl = API.replaceAll("<address>", URLEncoder.encode(address, DEFAULT_CHARSET));
            URL url = new URL(requestUrl);    // 把字符串转换为URL请求地址
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
            connection.connect();// 连接会话
            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {// 循环读取流
                sb.append(line);
            }
            br.close();// 关闭流
            connection.disconnect();// 断开连接
            System.out.println("Get=="+sb.toString());
            Matcher matcher = pattern.matcher(sb.toString());
            if (matcher.find() && matcher.groupCount() == 2) {
                double[] gps = new double[2];
                gps[0] = Double.valueOf(matcher.group(1));
                gps[1] = Double.valueOf(matcher.group(2));
                System.out.println(gps);
                return gps;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败!");
            return null;
        }
        return null;
    }

    /**
     * 接口调用  POST
     */
    public static double[] httpURLConnectionPOST (String address) {
        try {
            String requestUrl = API.replaceAll("<address>", URLEncoder.encode(address, DEFAULT_CHARSET));
            URL url = new URL(requestUrl);

            // 将url 以 open方法返回的urlConnection  连接强转为HttpURLConnection连接  (标识一个url所引用的远程对象连接)
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此时cnnection只是为一个连接对象,待连接中

            // 设置连接输出流为true,默认false (post 请求是以流的方式隐式的传递参数)
            connection.setDoOutput(true);

            // 设置连接输入流为true
            connection.setDoInput(true);

            // 设置请求方式为post
            connection.setRequestMethod("POST");

            // post请求缓存设为false
            connection.setUseCaches(false);

            // 设置该HttpURLConnection实例是否自动执行重定向
            connection.setInstanceFollowRedirects(true);

            //这是重点^_^
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            // 建立连接 (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
            connection.connect();

            // 创建输入输出流,用于往连接里面输出携带的参数,(输出内容为?后面的内容)
            DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
            String parm = "storeId=" + URLEncoder.encode("32", "utf-8"); //URLEncoder.encode()方法  为字符串进行编码

            // 将参数输出到连接
            dataout.writeBytes(parm);

            // 输出完成后刷新并关闭流
            dataout.flush();
            dataout.close(); // 重要且易忽略步骤 (关闭流,切记!)

            System.out.println(connection.getResponseCode());

            // 连接发起请求,处理服务器响应  (从连接获取到输入流并包装为bufferedReader)
            BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            System.out.println(bf.readLine());
            String line;
            StringBuilder sb = new StringBuilder(); // 用来存储响应数据

            // 循环读取流,若不到结尾处
            while ((line = bf.readLine()) != null) {
                sb.append(bf.readLine());
            }
            bf.close();    // 重要且易忽略步骤 (关闭流,切记!)
            connection.disconnect(); // 销毁连接
            Matcher matcher = pattern.matcher(sb.toString());
            if (matcher.find() && matcher.groupCount() == 2) {
                double[] gps = new double[2];
                gps[0] = Double.valueOf(matcher.group(1));
                gps[1] = Double.valueOf(matcher.group(2));
                return gps;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    public static void main(String[] args) {
//        httpURLConectionGET("安徽省合肥市政务文化新区蔚蓝商务港");
//        httpURLConnectionPOST("安徽省合肥市政务文化新区蔚蓝商务港");


        StringTokenizer tok = new StringTokenizer("", ",");
        System.out.println(tok.countTokens());
    }
}
