旧城不覆
2018-08-12 07:19
采纳率: 60%
浏览 5.7k
已采纳

求java通过http请求获取json数据的demo

java获取这个网址的json数据并解析http://www.yemaxgo.com/connect.php

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

10条回答 默认 最新

  • 躲在车里的猫 2018-08-12 07:53
    最佳回答

    package com.binjava.test;

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;

    public class MainFrame{

    public static void main(String[] args) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;
        try {
            URL url = new URL("http://www.yemaxgo.com/connect.php");
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(15000);
            connection.setReadTimeout(60000);
            connection.connect();
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
                result = "{1:"+result+"}";//不是严格的JSON数据格式,补成标准的json数据格式
                JsonParser jParser = new JsonParser();
                JsonObject jt = (JsonObject) jParser.parse(result);
                JsonArray jArray = jt.get("1").getAsJsonArray();
                JsonObject subObject=jArray.get(0).getAsJsonObject();
                System.out.println("id值为:"+subObject.get("id").getAsInt());
                System.out.println("speed值为:"+subObject.get("speed").getAsInt());
                System.out.println("pm25为:"+subObject.get("pm25").getAsInt());
                System.out.println("temperature(温度)为:"+subObject.get("temperature").getAsInt());
                System.out.println("humidity值为:"+subObject.get("humidity").getAsInt());
                System.out.println("temperature_warning值为:"+subObject.get("temperature_warning").getAsInt());
                System.out.println("gas_warning值为:"+subObject.get("gas_warning").getAsInt());
                System.out.println("humidity_warning值为:"+subObject.get("humidity_warning").getAsInt());
                System.out.println("lowpower_warning值为:"+subObject.get("lowpower_warning").getAsInt());
                System.out.println("mode值为:"+subObject.get("mode").getAsInt());
                System.out.println("timing_hour值为:"+subObject.get("timing_hour").getAsInt());
                System.out.println("timing_minute值为:"+subObject.get("timing_minute").getAsInt());
                System.out.println("timing_second值为:"+subObject.get("timing_second").getAsInt());
                System.out.println("update_time值为:"+subObject.get("update_time").getAsString());
                subObject=jArray.get(1).getAsJsonObject();
                System.out.println("time值为:"+subObject.get("time").getAsString());          
            }
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    

    }
    }
    图片说明

    评论
    解决 无用
    打赏 举报
查看更多回答(9条)

相关推荐 更多相似问题