如何使用JSON POST从openTSDB中读取?

时间:2015-04-16 06:16:06

标签: json http post request opentsdb

我在虚拟机上运行openTSDB,想要从客户端使用JSON POST请求读取数据。

Baseurl是我的VM的URL。

每当我发送此请求时,我都会收到错误消息"无法传递给定的JSON"在我的openTSDB中。那我的要求有什么不对?

提前致谢。

编辑:Json-Request应该代表这个GET请求(有效):

baseURL时+" / API /查询开始= 1356994800000&安培; M =总和:Leistung&#34 ;;

        String url = baseurl;
        URL obj = new URL(url + "/api/query");


        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        //add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-type", "application/json; charset=utf-8");
        con.setRequestProperty("Accept", "application/json; charset=utf-8");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");



        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeChars("{\n" +
                "    \"start\": 1356994800000,\n" +
                "    \"queries\": [\n" +
                "        {\n" +
                "            \"aggregator\": \"sum\",\n" +
                "            \"metric\": \"Leistung\"\n" +
                "        }\n" +
                "    ]\n" +
                "}");
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();


        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        con.disconnect();
        in.close();
        System.out.println(response.toString());

0 个答案:

没有答案