如何从Java中的Http Get请求获取JSON列表?

时间:2019-03-12 19:17:13

标签: java rest

我有一个返回JSON响应的网址,以下是该代码:

    String usepass = "XXXXXX" + ":" + "XXXXXXx#";
            String basicAuth = "Basic "
                    + javax.xml.bind.DatatypeConverter.printBase64Binary(usepass.getBytes());


            String url = "http://XXXXXXX/jira/rest/api/2/issue/CQ-12836?expand=changelog";

            URL obj = new URL("http://XXXXXXX/jira/rest/api/2/issue/CQ-12836?expand=changelog");
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            // optional default is GET
            con.setRequestMethod("GET");
            //add request header
            con.setRequestProperty("Authorization", basicAuth);
            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'GET' 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);
              }
             String jsonRespose = response.toString();
             in.close();
             //print in String
             System.out.println("*****print json*****");

但是,现在,如果更改返回JSON列表而不是单个JSON列表的URL,那么我该如何在Java中处理呢?我必须在这里进行哪些更改?

请在这里帮助我。

0 个答案:

没有答案