谁能解释这段代码在做什么

时间:2017-11-13 11:58:15

标签: java html jsoup mediawiki-api

我在这里遇到两个问题:

以下代码块让我感到困惑。首先,我不知道代码从基础知识到底做了什么;我只是从教程中复制它,它似乎做了我想要它做的事情。如果有人能够解释它的作用,那将非常有用。

第二个问题是我不知道它为什么会抛出ArrayIndexOutOfBounds错误,可能是因为我不理解它或其他原因。我真的需要澄清。

   try {
        Document searchLink = Jsoup.connect("https://www.google.com.ng/search?dcr=0&source=hp&ei=5-cIWuZ30cCwB7aUhrAN&q=" + URLEncoder.encode(searchValue, encoding))
                .userAgent("Mozilla/5.0").get();
        String websiteLink = searchLink.getElementsByTag("cite").get(0).text();


        //we are setting the value for the action "titles" in the wikipedia API with our own article title
        //we use the string method replaceAll() to remove the title of the article from the wikipedia URL that we generated from google
        //
        String wikiAPItoSearch = "https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=" 
                                + URLEncoder.encode(websiteLink.replaceAll("https://en.wikipedia.org/wiki/", ""),encoding);
        System.out.println(wikiAPItoSearch);

        //extraction of textfiles
        //from this point till down i cant really grab what is happening
        HttpURLConnection httpconn = (HttpURLConnection) new URL(wikiAPItoSearch).openConnection();
        httpconn.addRequestProperty("userAgent", "Mozilla/5.0");

        BufferedReader bf = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));

        //read line by line
        String response = bf.lines().collect(Collectors.joining());
        bf.close();
        ///it returns ArrayIndexOutOfBounds here
        String result = response.split("\"extract\":\"")[1];
        System.out.println(result);
    } catch (IOException e) {
        // TODO: handle exception
        e.printStackTrace();
    }

2 个答案:

答案 0 :(得分:1)

我认为没有人会花时间为您解释代码。一个很好的机会让你做一些调试。

ArrayIndexOutOfBounds来自response.split("\"extract\":\"")[1]。无法保证String response可以分成至少两部分。

添加检查以避免错误。而不是......

    String result = response.split("\"extract\":\"")[1];

使用...

    String[] parts = response.split("\"extract\":\"");
    String result;
    if (parts.length >= 2) {
        result = parts[1];
    } else {
        result = "Error..." + response; // a simple fallback 
    }

分裂是如何运作的:

String input = "one,two,three";
String[] parts = input.split(",");
System.out.println(parts[0]); // prints 'one'
System.out.println(parst[2]); // prints 'three'

所以在你的情况下,[1]表示parts数组中的第二项。 “\”extract \“:\”“必须在响应中至少出现一次,否则在parts数组中只有一个项目,当你尝试到达第二个项目时会出现错误(因为它没有因为.split接受正则表达式字符串并且“\”extract \“:\”“包含正则表达式保留字符。所以这一切都变得非常棘手。

答案 1 :(得分:0)

OPPS ......我意识到这是我正在使用的API导致错误,我从维基媒体获得的API不使用/ extract /作为delimetre,所以我检查了其他堆栈溢出文章以获得更清晰的API尤其是使用/ extract /作为API响应的delimetre的那个。

这是我得到的新API:

https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=

这是导致错误的前一个:

https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=

我认为这个错误是由于我无法理解该部门的过程而引起的。感谢您的回复。