I am having two problems here:
The following block of codes has got me confused. Primarily, I do not know what exactly the code is doing from the basics; I just copied it from a tutorial, and it seems to do what i want it to do. If anyone can explain in bits what it does, it will be really helpful.
The second problem is that I do not know why it throws an ArrayIndexOutOfBounds
error, maybe because I do not understand it or otherwise. I really need clarification.
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();
}