java调用第三方天气预报API接口

java调用第三方天气预报API接口

package com.sensordata.controller;
import com.common.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * @Author: Yang JianQiu
 * @Date: 2019/4/26 11:34
 * jdk类HttpURLConnection调用第三方http接口
 *
 * 通常分get和post两种方式
 *
 * 参考资料:
 * http://www.cnblogs.com/angusbao/p/7727649.html
 */
public class demo1 {

    /**
     * 以post或get方式调用对方接口方法,
     * @param pathUrl
     */
    public static void doPostOrGet(String pathUrl, String data){
        OutputStreamWriter out = null;
        BufferedReader br = null;
        String result = "";
        try {
            URL url = new URL(pathUrl);
            //打开和url之间的连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //请求方式
            conn.setRequestMethod("GET");
            //conn.setRequestMethod("GET");

            //设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");

            //DoOutput设置是否向httpUrlConnection输出,DoInput设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(5000); // 5秒 连接主机的超时时间(单位:毫秒)
            conn.setReadTimeout(5000);// 5秒 从主机读取数据的超时时间(单位:毫秒)
            /**
             * 下面的三句代码,就是调用第三方http接口
             */
            //获取URLConnection对象对应的输出流
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            //发送请求参数即数据
            out.write(data);
            //flush输出流的缓冲
            out.flush();

            /**
             * 下面的代码相当于,获取调用第三方http接口后返回的结果
             */
            //获取URLConnection对象对应的输入流
            InputStream is = conn.getInputStream();
            //构造一个字符流缓存
            br = new BufferedReader(new InputStreamReader(is));
            JSONObject dd1 = new JSONObject();
            JSONObject jb = new JSONObject();
            String str = "";
            char[] chs = new char[300];
            while ((str = br.readLine()) != null){
                result += str;
                System.out.println(str);
            }

            String aa = "";
            String[]  strs = result.split(",");
            for(int i=0,len=strs.length;i<len;i++){

                aa=strs[4].toString();

            }
            System.out.println("========="+aa.substring(9, 15) );
//            System.out.println(result);






            //关闭流
            is.close();
            //断开连接,disconnect是在底层tcp socket链接空闲时才切断,如果正在被其他线程使用就不切断。
            conn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (out != null){
                    out.close();
                }
                if (br != null){
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        /**
         *手机信息查询接口:http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=手机号
              *      http://api.showji.com/Locating/www.showji.com.aspx?m=手机号&output=json&callback=querycallback
         */
        doPostOrGet("https://api.help.bj.cn/apis/weather2d/?id=广州", "");
    }
}

//运行结果----------------
{
"status": "0",
"city": "广州市",
"aqi": "65",
"pm25": "120",
"temp": "30~21℃",
"weather": "多云",
"wind": "无持续风向<3级",
"weatherimg": "//www.help.bj.cn/weather/img/name/duoyun.png",
"tomorrow": {
"temp": "30~21℃",
"weather": "多云",
"wind": "无持续风向<3级",
"weatherimg": "//www.help.bj.cn/weather/img/name/duoyun.png"
}}
=========30~21

你可能感兴趣的:(java,开发语言)