【问题标题】:How to parse Get Request Response?如何解析获取请求响应?
【发布时间】:2019-12-12 12:54:53
【问题描述】:

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

我已经创建了一个解析方法:

  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..

有什么建议可以继续吗?

【问题讨论】:

  • 是的,不要编写 JSON 解析器。使用 Jackson 或 Gson。

标签: java json tablerow get-request


【解决方案1】:

Java的JSON解析器有很多,例如Jacksonfastjson,所以不需要自己写JSON解析器。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 2016-12-12
    • 2017-08-27
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多