使用Java

时间:2018-08-30 10:59:18

标签: json rest jira jira-rest-api

我正在使用Java,我想在此URL中使用json:http://jiraserver/rest/dev-status/latest/issue/detail?issueId=13879&applicationType=stash&dataType=repository

在浏览器上,此url可以正常工作,并且我获取了所有需要的json数据,但是在我的Java程序中,我得到了

  

java.io.IOException:服务器返回URL的HTTP响应代码:400:   http://jiraserver/rest/dev-status/latest/issue/detail?issueId=13879&applicationType=stash&dataType=repository

public static void main(String[] args) {
            System.out
                .println(jsonGetRequest("http://jiraserver/rest/dev-status/latest/issue/detail?issueId=13879&applicationType=stash&dataType=repository
"));
          }

          private static String streamToString(InputStream inputStream) {
            String text = new Scanner(inputStream, "UTF-8").useDelimiter("\\Z").next();
            return text;
          }

          public static String jsonGetRequest(String urlQueryString) {
    String json = null;
        try {
          URL url = new URL(urlQueryString);
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
          connection.setDoOutput(true);
          connection.setInstanceFollowRedirects(false);
          connection.setRequestMethod("GET");
          connection.setRequestProperty("Content-Type", "application/json");
          connection.setRequestProperty("charset", "utf-8");
          connection.connect();
          InputStream inStream = connection.getInputStream();
          json = streamToString(inStream); // input stream to string
        } catch (IOException ex) {
          ex.printStackTrace();
        }
        return json;
      }
我想念什么吗?如果有任何简单的实现可以随意使用该网址

0 个答案:

没有答案