Java中如何从HTTP请求中获取JSON对象

Ben*_*ben 6 java json httprequest

我现在尝试使用 Java cord 中的 HTTP 请求获取 JSON 对象。

我想知道如何在下面的代码中获取响应或 JSON 对象。

请告诉我。

(在这个程序中,我尝试获取文章“纽约”的维基百科类别。)

 String requestURL = "http://en.wikipedia.org/w/api.php?action=query&prop=categories&format=json&clshow=!hidden&cllimit=10&titles=" + words[i];
 URL wikiRequest = new URL(requestURL);
 URLConnection connection = wikiRequest.openConnection();  
 connection.setDoOutput(true);  

                    /**** I'd like to get response here. ****/

 JSONObject json = Util.parseJson(response);
Run Code Online (Sandbox Code Playgroud)

kga*_*ron 2

Scanner scanner = new Scanner(wikiRequest.openStream());
String response = scanner.useDelimiter("\\Z").next();
JSONObject json = Util.parseJson(response);
scanner.close();
Run Code Online (Sandbox Code Playgroud)