Java - 如何在没有外部库的情况下获取URL数组数据

时间:2015-11-26 07:17:53

标签: java

我已使用以下代码

从java中的给定URL检索数据
 try{
            String id="01";
            //String fullURL="http://XXXX:XXX/XX/getdata/?id="+ id;

        URL u = new URL(fullURL);
            System.out.println(fullURL);
            HttpURLConnection huc = (HttpURLConnection) u.openConnection();
            System.out.println("Message :"+huc.getResponseMessage());
            JSONParser parser = new JSONParser();
            BufferedReader rd = new BufferedReader(new InputStreamReader(huc.getInputStream()));

            JSONArray a = (JSONArray) parser.parse(rd);

            for (Object o : a)
            {
                JSONObject device = (JSONObject) o;

                long kw = (long) device.get("value");
                System.out.println("Sensor value : "+kw);
                //getKw().setKw(kw);

                String ST= (String) device.get("ST");
                System.out.println("Sensor type : "+ST);


                //Timestamp dateTime = (Timestamp) device.get("serverTimeStamp");
                Timestamp dateTime = new Timestamp ((long) device.get("serverTimeStamp"));

                System.out.println("Date in long : "+device.get("serverTimeStamp"));
                System.out.println("Date : "+dateTime);
                //getServerTimeStamp().setServerTimeStamp(dateTime);
                System.out.println("*************");
  }

        }
        catch(Exception e){
            System.out.println("Error :"+e);
            e.printStackTrace();

        }

实际数据如下所示:

[
  {

    "value": 777,
    "percentage": 0,
    "serverTimeStamp": 1436900289000,
    "ST": "S"

  },
  {
    "value": 777,
    "percentage": 0,
    "serverTimeStamp": 1436900289000,
    "ST": "V"
  },
  {
    "value": 777,
    "percentage": 0,
    "serverTimeStamp": 1436900289000,
    "ST": "R"
  }
]

当我使用json.simple库时,它的工作非常好。但问题是,我应该在不使用任何外部库的情况下获取数据。

我如何仅在java代码中实现这一目标?

0 个答案:

没有答案