簡単なJavaで Watson Weather Company Data から天気予報を取得する



    public static void main(String[] args) throws Exception {

        String username = "xxx";
        String password = "xxx";

        URL url = new URL(
                "https://twcservice.mybluemix.net/api/weather/v1/geocode/35.681167/139.76705/forecast/daily/3day.json?language=ja-JP");

        URLConnection conn = url.openConnection();
        conn.setRequestProperty(
                "Authorization",
                "Basic "
                        + Base64.getEncoder().encodeToString(
                                (username + ":" + password).getBytes()));

Base64.getEncoder().encodeToString("username:password".getBytes()));
        InputStream is = conn.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(is,
                "UTF-8"));

        StringBuilder sb = new StringBuilder();

        String line;

        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        System.out.println(sb.toString());

        br.close();

    }