/**
     * 根据URL请求json数据
     * @return //parm:请求的url链接  返回的是json字符串
     */
    public static String getURLContent() {

        SysSetup sysSetup = (SysSetup) ResourceManage.getContext("sysSetup");
        String urlStr = (String) sysSetup.getParameter("WEATHER_ADDRESS");//获取天气数据接口地址
        //String urlStr = "http://www.pm25.in/api/querys/aqi_details.json?city=zhuhai&token=CkFW1K";

        //请求的url
        URL url = null;

        //建立的http链接
        HttpURLConnection httpConn = null;

        //请求的输入流
        BufferedReader in = null;

        //输入流的缓冲
        StringBuffer sb = new StringBuffer();

        try{
            url = new URL(urlStr);

            in = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8") );

            String str = null;

            //一行一行进行读入
            while((str = in.readLine()) != null) {
                sb.append( str );
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
        } finally{
            try{
                if(in!=null) {
                    in.close(); //关闭流
                }
            }catch(IOException ex) {
                System.out.println(ex.toString());
            }
        }
        String result =sb.toString();
        return result;
    }


    public static void main(String[] args){
        getURLContent();
    }

解析后可以得到URL返回的json字符串,如下图

JAVA 访问直接返回json的URL,并解析

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2021-06-11
猜你喜欢
  • 2021-07-14
  • 2021-09-28
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案