gpt4 book ai didi

java - 如何解析Get Request Response?

转载 作者:行者123 更新时间:2023-11-30 10:01:36 25 4
gpt4 key购买 nike

我正在尝试解析 json 数据,但我遇到了一个问题,它没有解析任何内容,也没有输出到我的表行对象中。

我创建了一个解析方法:

  public static String parseRequest(String request,Table Row) {
String array1 [] = request.split(":");
for (int i = 0; i < array1.length; i++) {
String[] array2 = array1[i].split(",");
}
return request;
}

这是我的 Http 请求:

public static void GetRequest() {

BufferedReader reader;
String access_token = "BlahBlahBlach";

String line;
StringBuffer responseContentReader = new StringBuffer();

try {

URL url = new URL("https://bigquery.googleapis.com/discovery/v1/apis/bigquery/v2/rest");
connection = (HttpsURLConnection) url.openConnection();

connection.setRequestProperty("X-Risk-Token ", access_token);


connection.setRequestProperty("Accept", "application/json");

//here we should be able to "request" our setup
//Here will be the method I will use
connection.setRequestMethod("GET");

//after 5 sec if the connection is not successful time it out
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);

int status = connection.getResponseCode();
//System.out.println(status); //here the connect was established output was 200 (OK)

//here we are dealing with the connection isnt succesful

if (status > 299) {
reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
while ((line = reader.readLine()) != null) {
responseContentReader.append(" ");
responseContentReader.append(line);
responseContentReader.append("\n");
}
reader.close();
//returns what is successful
} else {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
responseContentReader.append(" ");
responseContentReader.append(line);
responseContentReader.append("\n");
}
reader.close();
}
//System.out.println(responseContentReader.toString());
System.out.println(parseRequest(responseContentReader.toString(),row);


} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
connection.disconnect();
}

}

这是我的结果:

 {"vulnerability":{"id":6202813,"status":"open","closed_at":null,"created_at":"2019-06-18T19:45:13Z","due_date":null,"notes":null,"port":[],"priority":null,"identifiers":["adobe-reader-apsb10-28-cve-2010-3654"],"last_seen_time":"2019-07-30T05:00:00.000Z","fix_id":7109,"scanner_vulnerabilities":[{"port":null,"external_unique_id":"adobe-reader-apsb10-28-cve-2010-3654","open":true}]

但预期的输出应该是正在解析的 json 数据:

 id : 6202813
status: open
closed_at: null
etc..

有什么建议可以继续吗?

最佳答案

Java 有许多 JSON 解析器,例如 Jackson , 和 fastjson ,所以不需要自己写JSON解析器。

对于 HTTP 请求,okhttp是一个不错的选择。

关于java - 如何解析Get Request Response?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com